Matthew
Matthew

Reputation: 31

www to non-www redirection won't work

I'm writing a www to non-www redirection with the following code:

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

However, www.mywebsite.com redirects to mywebsite.com/www/ instead of mywebsite.com.

I suspect this might be due to the DocumentRoot configuration of a VirtualHost, but I cannot access the httpd.conf file since I'm on a shared environment.

Any ideas?

Upvotes: 3

Views: 132

Answers (1)

MrWhite
MrWhite

Reputation: 45829

As Olaf suggests in comments, normally this should not happen. However, there have been a few questions like this where the hosting account (in a shared environment) is somehow dependent on a parent configuration and enabling mod_rewrite inheritance resolves the problem:

RewriteOptions Inherit

Admittedly, how or why this should work is a bit of a mystery. (Particularly since parent directives are executed after child directives, etc. ?)

You can also try changing your RewriteRule to use the value of REQUEST_URI instead, for example:

RewriteRule ^ http://%1%{REQUEST_URI} [R=302,L]

Change to 301 when you are sure it's working OK. Permanent redirects are naturally cached by the browser so can make testing tricky. (Also, clear your browser cache before testing this!)

Upvotes: 1

Related Questions