Forum Moderators: phranque

Message Too Old, No Replies

trying to rewrite URL. works on some, but not others

         

schulerlab

7:43 pm on May 13, 2006 (gmt 0)

10+ Year Member



I have an old domain and a new domain both pointing to one site (on one host).

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]

schulerlab

7:45 pm on May 13, 2006 (gmt 0)

10+ Year Member



yo.. the htaccess formatted a bit weird in my last post..

my htaccess files have no underlines :\

sorry.

but you get the idea.

jdMorgan

12:02 am on May 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The second rule has an error in it. It says redirect anything that is NOT newdomain.com to www.newdomain.com. Therefore, this rule is likely to 'loop forever' until the borwser or server reaches its maximum redirection limit.

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]

Jim

schulerlab

6:55 am on May 14, 2006 (gmt 0)

10+ Year Member



ok.. I see what you are saying about the second rule.

but why would it loop? wouldn't it just go thru the test and rewrite once?

and thanks for the code that you posted. I tried it and it works great!

jdMorgan

1:33 pm on May 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> ...Why would it loop?

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]

  • HTTP Request received for www.newdomain.com/<anything>
  • RewriteCond tested. Domain does NOT start with newdomain.com
  • External Redirect to www.newdomain.com/<anything>
  • HTTP Request received for www.newdomain.com/<anything>
  • RewriteCond tested. Domain does NOT start with newdomain.com
  • External Redirect to www.newdomain.com/<anything>
  • Repeat until client or server redirection limit reached.


    Now, without a start anchor, the behaviour changes, but it's still not right:

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

  • HTTP Request received for newdomain.com/<anything>
  • RewriteCond tested. Domain *does* contain newdomain.com
  • RewriteRule is not invoked, so no non-www-to-www redirect will occur.

    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

  • schulerlab

    8:33 pm on May 14, 2006 (gmt 0)

    10+ Year Member



    ok.. I am so glad you explained! I didn't realize that if there was a match, with R=301 (that is the external redirect you are talking about, right?), that the Apache control program(?) would, in essense, leave the entire htaccess sequence, then create a new request, which would reinvoke the entire htaccess sequence again..hence the loop.

    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..

    jdMorgan

    11:02 pm on May 14, 2006 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Apache does not create another request for a 301 redirect. Remember, Apache is the server, not the client.

    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

    schulerlab

    11:51 pm on May 14, 2006 (gmt 0)

    10+ Year Member



    ok, I understand better now what is happening.

    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.