PDA

View Full Version : Rewriting example.com to www.example.com



rolling
09-21-2007, 08:26 AM
Can anybody see what I am getting wrong here?

<Directory "/var/www/html">
#
#Rewrite example.com to www.example.com
RewriteEngine on
RewriteCond %(HTTP_HOST) !www.example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=permanent]
</Directory>
I have tried various combinations, but while example.com gets redirected to http://www.example.com, so does www.example.com and I end up in an endless loop :(

I have tried
RewriteCond %(HTTP_HOST) !www.example.com [NC]
RewriteCond %(HTTP_HOST) !^www.example.com [NC]
RewriteCond %(HTTP_HOST) !^www\.example\.com [NC]
RewriteCond %(HTTP_HOST) example.com [NC]

PS These edits are being made in httpd.conf

wildjokerdesign
09-21-2007, 06:28 PM
I think you are missing a forward slash in your RewriteRule. Try this... it comes straight from the Apache Rewriting Guide (http://httpd.apache.org/docs/2.0/misc/rewriteguide.html):


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

I do know you have to escape the (dots) with a backslash in the RewriteCond. You don't do it in the RewriteRule when you are working in httpd.conf.

wildjokerdesign
09-21-2007, 07:07 PM
I ended up haveing to put the above inside of the VirtualHost tag so this is what worked for me:

<VirtualHost *:80>
<Directory "/var/www/html">
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R]
</Directory>
</VirtualHost>

hawzor
09-22-2007, 12:56 PM
Thanks for your tips wildjokerdesign, yours works as advertised. However, if you have a look at the headers that this (I guess any) Rewrite produces, you will note that they contain the ever-so-poison 302 "Found" which kills robotic traffic because bots and spiders assume that this is a "park" and not a helpful "permanent" move.

To get the more SEO-loving 301 redirect "permanent" requires just that: RedirectPermanent using mod_alias.

In other Apache 2 setups I have worked with, the following has been a charm, just not here for some reason at this otherwise very excellent host.

Of course using my IP:


ServerName www.example.com

###

NameVirtualHost 111.222.111.222:80

<VirtualHost 111.222.111.222:80>
ServerName example.com
RedirectPermanent / http://www.example.com/
</VirtualHost>


The "break" I am encountering with this setup is this: say I go to my browser and type in url just as "example.com" with the expectation that Apache will redirect the url to "http://www.example.com", this works on the first step fine, gives a nice 301 header, reloads the "www.example.com", but then apparently gets confused about the ServerName because Apache then RE-redirects the www.example.com (it should not because the VirtualHost is only the naked url, sans www.) to redirect again to www. a bazillion more times in a loop until my browser calls it quits, loading nothing.

I have also tried the foolowing:


NameVirtualHost *:80

<VirtualHost *:80>
ServerName example.com
ServerAlias *.example.com
RedirectPermanent / http://www.example.com/
</VirtualHost>

No help, same thing.

Note: Both domains have correct A records in DNS which is serving right. UseCanonicalNames is Off.

Any advice lurking out there? Thanks much.

wildjokerdesign
09-22-2007, 03:41 PM
Well you lost me now. :) This really is not my area and kind of only lucked out by trying different things.

What happens if you add the R=permanent to the RewriteRule? Well that satisfy the SEO's.

I would have to do some digging to come up with anything more and I don't have the time right now. I'll try to come back to this if I can find the time.

rolling
09-22-2007, 07:30 PM
Thanks for the replies Shawn. I've ended up with

<Directory "/var/www/html">
#
#Rewrite example.com to www.example.com 21-Sep-2007
RewriteEngine on
#IF case insensitive BaseURL does NOT start www.example.com
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
#AND IF case insensitive BaseURL is NOT blank
RewriteCond %{HTTP_HOST} !^$ [NC]
#THEN rewrite BaseURL as www.example.com, returning code 301 and stop processing
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,L]
</Directory>

which seems to work after a fashion, but while the URL gets rewritten in my browser, I get status code 200 when accessing either example.com or www.example.com. The critical difference between your solution and mine is the addition of the NOT blank line.

Guess I have some more digging to do to get a 301 for example.com and 200 for www.example.com

hawzor
09-22-2007, 08:38 PM
Hi, I was bugging on this same issue most of today. Found some good advice out there and this is a shorter version that is working for me, albeit, not as clean as the old VirtualHost RedirectPermanent I previously loved.


RewriteCond %{HTTP_HOST} !^www\.example\.com$

# If your site isn't being asked for politely with a nice www

RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]

#Rewrite the whole string start-to-finish: ^(.*)$
#with the new URL as array element 1, and
#redirect with 301 (permanent) header.

I have been now bugging on another (tangentially related) matter and I should probably repost somewhere else, but since everybody is in the mod_rewrite mood....I will give this a go.

I have come to Westhost to migrate my client OUT of a horrifying hosting solution (under ASP) and into the LAMP light. Problem: they have been SEO'd and indexed. This means that to stay kosher with like, say, Google, the .asp files will need permanent redirecting. Not so bad, I have a long list, but it works. But this is the worse problem: many are dynamic pages with query strings attached.

We don't care about the content of the old query strings really, we have a new schemas and we will just take those people to a main directory and have them re-navigate the new site. But Apache preserves these query strings on redirect and attaches them meaninglessly to static files and php files, etc. The worry is the bots who receive the 301 permanent redirects WITH the query strings attached will THINK the strung-out URL is a legitimate new home for stuff.

I have tried a bazillion RewriteCond and RewriteRules and nothing works to get my Apache to kill the query string and allow me to proceed to RedirectMatch permanent without 404-ing that the .asp file itself is just simply not present on the server.

Somebody must have done a migration like this before. Please say yes?

Or, alternatively, some SEO guru please tell me I am fretting the bots for no good reason and can go back to PHP-land...happily.