Forum Moderators: phranque
I am trying to write my .htaccess file so the people with links to the old site, will go to the new site (which they will because I have the DNS name servers pointed to the one site)... but I want the user to see the new domain in the url.
I also want links in the form "newdomain.com" to be transformed to "www.newdomain.com".
what I have works for changing the old domain to the new.. but doesn't for forcing www.newdomain.com..
what am I doing wrong?
and a 2nd minor question.. do I need to turn the rewrite engine on and off for each transformation (I am now)
here is what I have in my htaccess file:
AddType 'text/html; charset=UTF-8' html
ErrorDocument 404 /404.html
Options +FollowSymLinks
# newdomain.com/ ==> www.newdomain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^newdomain\.com
RewriteRule (.*) http://www.newdomain.com/$1 [L,R=301]
#?olddomain.com/ ==> http://www.newdomain.com/
RewriteEngine on
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [L,R=301]
# prevent hotlinking
RewriteEngine on
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?newdomain\.com [NC]
RewriteRule \.(gif¦jpg)$ http://newdomain.com/i/graphictheft.jpeg [L,R=301]
[edited by: jdMorgan at 11:54 pm (utc) on May 13, 2006]
[edit reason] de-linked [/edit]
You do not need to enable the rewrite engine for every rule, just once at the beginning
Your first rule is not needed at all, since the second rule (if corrected) will handle both oldddomain.com and the non-canonical newdomain.com
Rolling all this together yields:
AddType 'text/html; charset=UTF-8' html
#
ErrorDocument 404 /404.html
Options +FollowSymLinks
RewriteEngine on
#
# Redirect anything that's NOT www.newdomain.com to www.newdomain.com
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.newdomain\.com
RewriteRule (.*) http://www.newdomain.com/$1 [L,R=301]
#
# prevent hotlinking
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?newdomain\.com [NC]
RewriteRule \.(gif¦jpg)$ http://newdomain.com/i/graphictheft.jpeg [L,R=301]
I misread the code, and did not note that the RewriteCond pattern was not start-anchored. If it were, here is the sequence:
RewriteCond %{HTTP_HOST} !^newdomain.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [L,R=301]
This is likely the reason you ended up with two rules when only one was needed.
Anyway, sorry for the confusion -- Old eyes gettin' blurry at night.
Jim
it would not be an internal loop (within this one invokation of htaccess), but a loop because this one invokation of htacces would create another request.. then another invokation of htaccess, ad infinitim or ad htaccess max limit.
please keep posting.. blurry eyes and all! ;)
I read the Apache documents (several times).. but some of these subtleties don't hit you until your logic doesn't work. LOL
Thanks again.
now to look up what webmasterworld says about logging these requests..
When it encounters [R=301] on a mod_rewrite RewriteRule directive, or executes the similar Redirect 301 directive in mod_alias, Apache generates a Redirect response containing the new URL and sends it to the client (browser). It is then up to the browser to re-issue it's request for the desired resource using the new URL supplied in the server's redirect response. It is the client that creates the new HTTP transaction.
Jim
I have worked with other operating systems, but to a person with a site on a multi site host, who does what gets a little hazy sometimes. Especially when you can't see much of what goes on above your site level.
But what you said makes it much clearer for me.
Thanks again.