Forum Moderators: phranque

Message Too Old, No Replies

Global non-www to www redirect

Attempting to solve the issue in the central httpd.conf

         

lammert

8:14 pm on Feb 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



non-www to www redirects is one of the most discussed uses of the mod_rewrite facility in Apache. At this moment I have several sites running on the same server and I have full access to the httpd.conf file. My goal is to get rid of all single .htaccess files and move the non-www to www redirect to the httpd.conf.

After some playing with rewrite rules, I came up with the following idea.

1. Test if HTTP_HOST matches a pattern ^part1.part2$
2. If the above is the case, replace it with www.%{HTTP_HOST}.

Because I check if the HTTP_HOST matches two parts with a central dot, I won't add www to existing sub-domains. This is the code I want to use. Any problems or things I didn't look after yet? It seems so simple and would allow me to remove a dozen or so .htaccess files.


RewriteCond %{HTTP_HOST} ^[^\.]+\.[^\.]+$
RewriteRule ^/(.*)$ h**p://www.%{HTTP_HOST}/$1 [R=301,L]

jdMorgan

1:51 am on Feb 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looks OK to me... Try it!

(You could put it into one VirtualHost container for a low-impact test, rather than testing globally first.)

Jim

lammert

1:52 pm on Feb 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have tried it in a VirtualHost container and it works. The only problem is that I can't get it to work globally when I put it outside of the container :(

jdMorgan

2:06 pm on Feb 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you tried a different type of container, like <Directory>, etc.?

See the Context description under the documentation for each Apache module directive. This specifies the allowable contexts for each directive. For RewriteRule, the context is "server config, virtual host, directory, .htaccess". So it should work within a <Directory> container, and within a server config container.

Jim

lammert

6:31 pm on Feb 15, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I first couldn't get it to work outside a <VirtualHost> container. Then I made a new virtual host with the server name "www-redirect" and as aliases all the aliases I wanted to redirect to their www counterpart. I have now the following content in my httpd.conf and it works perfectly. I am tracking all the virtual hosts in the access_log and the entries starting with "www-redirect" are now all 301's to the actual site content.

<VirtualHost *:80>
ServerName www-redirect
ServerAlias site1.com
ServerAlias site2.com
ServerAlias ...
ServerAlias siteN.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^\./]+\.[^\./]+$
RewriteRule ^/(.*)$ h**p://www.%{HTTP_HOST}/$1 [R=301,L]
</VirtualHost>

<VirtualHost *:80>
ServerName www.site1.com
...
</VirtualHost>

<VirtualHost *:80>
ServerName www.site2.com
...
</VirtualHost>
...

jdMorgan

11:54 pm on Feb 15, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A very clever solution!

Jim