This project is read-only.

Rewrite rule to redirect to www or non-www

Jan 31, 2013 at 8:35 AM
Edited Jan 31, 2013 at 8:38 AM
As you all know it's a good (seo) thing to choose for www-urls or non www-urls. Just choose one. I'm trying to accomplish this with the Rewrite Rules module. I think it's nice not to have to change the web-config / IIS settings. I used the module from the gallery, version 1.2.5.

This seems to work fine in case of redirecting to urls with 'www':
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R=301,L]
I chose to eliminate the 'www' subdomain. It makes the url shorter and the 'dub dub dub' is actually not necessary. If someone uses 'www' I want to redirect. So, now to find a working rewrite rule. Preferably a universal on that is applicable for any domain name.

This rule seems to work fine for a specific domain example.com :
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?example\.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
The following rules do not seem to work (gives an extra slash)
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [C]
RewriteRule ^www\.(.*)$ http://$1 [R=301,L]
And also this one does not work
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
The goal here is to find a good (working) rule and let others profit from it. If someone really understands this grammar already, don't hesitate to explain.
Mar 29, 2013 at 6:17 PM
I have the same need. geertdoornbos, were you able to come up with a solution? Basically, I have the following needs:

All requests to the following domains should redirect to http://mysite.com
Any clue how to write the rules to accomplish this?
Mar 30, 2013 at 11:55 AM
Hi bmccleary,

All I needed was a redirect from www.example.com to example.com, so I used this:
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?example\.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
The rewrite rules are some form of regular expression derivate. This stuff is too much magic for me :-)
There are websites where you can generate rewrite rules, but not all rules seem to work correctly in the 'Orchard Rewrite Rule' module.
I guess you have to fiddle a bit to get the correct rules.
Hope you're willing to share your results here.