Forum Moderators: phranque

Message Too Old, No Replies

check if URL-path-part is present in request

add it if not

         

angelo moreira

2:19 pm on Jun 9, 2010 (gmt 0)

10+ Year Member



Good afternoon,

Long day today, I have a problem of performance on a live website, I decided to download the website as flat HTML so I can have time do perform tests on the app without having the client upset.

My problem is that when I do this migration I don't wanna have any of the google links going to pages that don't exist.

basically i wanna redirect this:
mywebsite.com/mypage to
mywebsite.com/en-GB/mypage.html

so far I could get to work the code to redirect from
mywebsite/mypage and
mywebsite/mypage/

to
mywebsite/mypage.html


My mod_rewrite knowledge is non existent but I'm trying as hard as I can to learn it.

So far I have this:

RewriteCond %{HTTP_HOST} !^en-GB$ [NC]
RewriteRule .? en-GB%{REQUEST_URI} [R=301,L]


this goes into a infinite loop and I don't understand why.
On my newbie logic it says "if u dont have en-GB on the URL redirect to en-GB + original url"

please help me this is really urgent and I have to figure it out as fast as I can.

Many thanks,
Angelo

angelo moreira

4:00 pm on Jun 9, 2010 (gmt 0)

10+ Year Member



I'm desperate I just don't get it.
this works fine for me:
Rewriterule ^example/(.*)$ en-GB/$1 [QSA,L,R=302]

example/something goes to en-GB/something

when I modify it to:
Rewriterule ^(.*)$ en-GB/$1 [QSA,L,R=302]

Just goes into a infinite loop, is this syntax not suppose to look at the url and just add the en-GB/ ?

Anyone can help me please.....

jdMorgan

5:40 pm on Jun 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It loops because the subsequent (redirected) HTTP request for en-GB/<any-page> matches the rule again, and so is redirected again. Add a RewriteCond to stop the loop by skipping the rule if the requested URL-path already starts with "en-GB/":

RewriteCond $1 !^en-GB/
Rewriterule ^(.*)$ http://www.example.com/en-GB/$1 [R=302,L]

The [QSA] flag is not needed, since you are not appending any new query data. Any existing query data will pass through this rule unaffected.

Any external redirect (as you have coded with R=302) should provide a protocol and the canonical domain as shown.

[added] You almost had this almost in your original code, but you checked the wrong server variable. HTTP_HOST contains the requested hostname (loosely, the 'domain name'), and does not contain any part of the requested URL-path. There were a few other problems, but the whole concept is now outlined in the title of this thread. [/added]

Jim