Forum Moderators: phranque

Message Too Old, No Replies

www wildcard redirection

www

         

shib4u

3:39 pm on Apr 12, 2011 (gmt 0)

10+ Year Member



I have a Apache webserver with multiple virtual hosts. I want to redirect all my non-www users to the www website. How do I do this?

I cannot hard code the website host in the RewriteRule becuase I have around 10 vhosts. All those vhosts point to the same Directory. The application is such that the website changes according to the domain referred.

The following code strips off www from the website, but i want the reverse to happen, i.e. I want www to be added to the host url if it is not specified.


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

shib4u

4:02 pm on Apr 12, 2011 (gmt 0)

10+ Year Member



Found it myself...


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

g1smd

11:04 pm on Apr 12, 2011 (gmt 0)

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



Slightly simpler...

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

jdMorgan

4:12 pm on Apr 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd also like to add a warning that those sites had better change *a lot* based on the requested domain. If they do not, then you are essentially investing a lot of time and money into creating domains that compete with each other (duplicate content) -- In effect you may be competing with yourself!

If these sites have "similar" content, then they should all be merged and redirected to a single site.

It's important to consider the "SEO effects" of code as well as the server-side effects.

Jim

g1smd

8:26 pm on Apr 14, 2011 (gmt 0)

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



I hadn't considered whether this is a person hosting websites for 10 different small businesses or one person with 10 sites flogging the same or similar items. That is an important point.

We often don't see the full story laid out in the original question.