Forum Moderators: phranque

Message Too Old, No Replies

Looking for .htaccess rewrite code to add www - but for any domain

         

darrenlambert

5:30 pm on Jan 24, 2006 (gmt 0)

10+ Year Member



Hi. I have seen lots of examples of .htaccess rewrite code that will redirect any mydomain.com requests to www.mydomain.com. However I would like an example where one doesn't have to code the actual domain name, as I have a single script installation being used by many web sites, and therefore cannot hard code the domain name in the .htaccess file.

Does anyone have an example?
Thanks

extras

7:08 pm on Jan 24, 2006 (gmt 0)

10+ Year Member



Here.
[webmasterworld.com...]

If you only need it for http, this should do.


Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST}!^(www\.¦$) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This code takes care of missing trailing slash problem, too.


Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI}\\/%{HTTP_HOST}/www. ^/+(.+/)?[^.]*[^/]\\(/)([^w][^w][^w][^.].*/(www\.)¦.*)$ [OR,NC]
RewriteCond %{HTTP_HOST}/www. ^(/)?(/)?([^w][^w][^w][^.].*/(www\.))$ [NC]
RewriteRule ^ http://%4%{HTTP_HOST}%{REQUEST_URI}%2 [L,R=301]

darrenlambert

10:22 am on Jan 25, 2006 (gmt 0)

10+ Year Member



extras, many thanks, that is perfect.