0

I am running a tomcat server on a vps server which is having centos. My website is browsing with mydomain.com and www.mydomain.com. But I don't want to see www in my url. i.e. If I browse with www.mydomain.com, it should be redirected to mydomain.com.

For this thing, I have added Rewrite valve in my context.xml and also created rewrite.config file in /opt/apache/conf/Catalina/mydomain.com and added below rewrite rules

RewriteCond %{HTTP_HOST} ^www.mydomain.com [NC]

RewriteRule ^(.*)$ mydomain.com/$1 [L,R=301]

But it is not working. When I browse with www.mydomain.com, the url stays the same. It is not redirecting to mydomain.com

Please suggest me where I was doing wrong.

Nagkings
  • 31
  • 1
  • 2
  • same goes with below ? RewriteEngine On RewriteCond %{HTTP_HOST} www.yourwebsitehere.com RewriteRule (.*) http://yourwebsitehere.com/$1 [R=301,L] – Aravinda Feb 12 '20 at 04:02
  • Not working. Still I can see www in my url after browsing. – Nagkings Feb 12 '20 at 04:50
  • The `RewriteValve` can be configured in two ways: 1. you put the valve in the `` block and `rewrite.config` in `$CATALINA_BASE/conf/Catalina/` or 2. you put the valve in your webapp `context.xml` and `rewrite.config` in the `WEB-INF` folder of you app. @Aravinda Tomcat's `RewriteValve` does not need/support `RewriteEngine on`. – Piotr P. Karwasz Feb 12 '20 at 04:51
  • Yeah. I followed the second one. But no use. – Nagkings Feb 12 '20 at 04:52
  • In the question you say you mixed up the two ways. Increase the logging level of `org.apache.catalina.valves` to get some feedback. – Piotr P. Karwasz Feb 12 '20 at 04:55
  • Does this answer your question? [How do I remove the www from my domain URL](https://serverfault.com/questions/1002167/how-do-i-remove-the-www-from-my-domain-url) – Piotr P. Karwasz Feb 29 '20 at 05:55

1 Answers1

0

Firstly your rewrite rule has a small error. It should use a full URL for the redirect (cf. description of redirect flag):

RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com$1 [L,R=301]

However this doesn't explain why the rewrite rule is not triggered. As mentioned in the comment above, you must make sure that the rewrite.config file is in the write place. With the default server.xml configuration (engine name Catalina, host name localhost) you need to put it in:

  • $CATALINA_BASE/conf/Catalina/localhost if you added the RewriteValve to the <Host> element of your server.xml.
  • the WEB-INF folder of your application, if you added the RewriteValve to the <Context> element of your application.

In any case it might help you to increase the log level of the Servlet logger:

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = FINE

in $CATALINA_BASE/conf/logging.properties, which will log to localhost.<date>.log the lines:

Read configuration from: ...
Add rule with pattern ^(.*)$ and substitution http://example.com$1 
Add condition ^www.example.com test %{HTTP_HOST} to rule with pattern ^(.*)$ and substitution http://example.com$1 [NC] 

whenever the RewriteValve is properly configured.

Piotr P. Karwasz
  • 5,748
  • 2
  • 11
  • 21