3

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?

MrWhite
  • 43,179
  • 8
  • 60
  • 84
Matthew
  • 31
  • 4
  • 1
    What is the full URL of the request? And this .htaccess file is presumably in the document root? – MrWhite Nov 15 '15 at 22:40
  • The rule should work as is. The described behaviour can only happen, when you request `http://www.mywebsite.com/www`. Most likely, there's more in your configuration. – Olaf Dietsche Nov 16 '15 at 01:07
  • @w3d: full URL is `http://www.mywebsite.com`. Thanks – Matthew Nov 17 '15 at 00:09

1 Answers1

1

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!)

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • thanks for your answer. Investigating further based on your proposal, I discovered that RewriteRule's condition was causing the trouble. Things work properly with `RewriteRule ^ http://%1/$1 [R=302]`, or `RewriteRule ^ http://%1%{REQUEST_URI} [R=302]`as well, though `www.mywebsite.com/something`doesn't redirect to `mywebsite.com/something`. – Matthew Nov 17 '15 at 00:08
  • Just to note, `RewriteRule ^ http://%1/$1 [R=302]` would **not** work, it would need to be the 2nd directive with `REQUEST_URI` if you are using the pattern `^`. The first directive would simply strip the URL-path, so no `www` - but no _anything_! However, are you saying the 2nd one does not work either... are you still losing the `something`? Incidentally, you should use the `L` flag (in case you have any other directives). – MrWhite Nov 17 '15 at 00:32
  • Do you have any other directives in your .htaccess file? – MrWhite Nov 17 '15 at 00:33
  • Ok. Indeed: the first one (`www.mywebsite.com` to `mywebsite.com`) works whereas the second one doesn't (`www.mywebsite.com/something`doesn't redirect to `mywebsite.com/something`). The other directives of the .htaccess file are: `Options All -Indexes`, `ServerSignature Off`, `AddDefaultCharset UTF-8`, ` order allow,deny deny from all satisfy all ` and `ErrorDocument 401 /errors/401.html` (idem 403, 404 and 500). Thanks – Matthew Nov 17 '15 at 07:05
  • Dear @w3d: does the input help you finding an explanation to this behavior? – Matthew Nov 19 '15 at 21:57