Forum Moderators: phranque

Message Too Old, No Replies

Appending the request uri to a redirected url

         

lokeshshettyk

6:33 am on Nov 18, 2008 (gmt 0)

10+ Year Member



Hi,
I am redirecting all the requests from a host(http://sub1.example.com/) to another(http://sub2.sub1.example.com/) based on the user agent. If the user agent is mobile device i wanna redirect that request.
Here is my code which works ALL FINE

# iPhone iPod redirects #
RewriteCond %{HTTP_USER_AGENT} .*Mobile.*Safari [NC]
RewriteCond %{HTTP_HOST} !^sub2\.
RewriteRule ^([^/]+)$ http://sub2.sub1.example.com [L]

No what I want is, if someone enters [sub1.example.com ] that should be redirected to [sub2.sub1.example.com ]

The bottom line is, the redirected url must have the REQUEST_URI appended from the previous URL

Appreciate your help on this

Cheers :)

g1smd

11:53 am on Nov 18, 2008 (gmt 0)

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



Be aware that you have a 302 redirect.

Your code contains a domain name in the target, so that is a redirect. You haven't specified [R=301] so you get a 302 redirect. That's the default behaviour for both "R missing" and "R without a number".

I would append

/$1 [R=301,L]
to the end of the target URL. The $1 reuses the backreference collected earlier, and the R=301 confirms it as a 301 redirect.

If the site does NOT use query strings at all, I would also add a question mark to the end of the target URL (after the $1 bit) to clear any query string that may have accidentally been appended by a user trying to make duplicate content URLs on your site.

By the way, your example URL ends with a trailing slash but your matching rule doesn't cater for that as far as I can see.

jdMorgan

2:32 pm on Nov 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I use something like this:

# Redirect iPhone and iPod from sub1.example.com to sub2.sub1.example.com
RewriteCond %{HTTP_HOST} ^sub1\.example\.com
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/5\.0\ \((iPhoneŠiPod);\ U;\ CPU\ (iPhone\ OS\ [0-9_]+\ )?like\ Mac\ OS\ X;\ [a-z]{2}(-[a-z]{2})?\)\ AppleWebKit/[4-9][0-9.]+\+?\ \(KHTML,\ like\ Gecko\)\ Version/[0-9.]+\ Mobile/[0-9A-Za-z]+\ Safari/[0-9.]+$
RewriteRule (.*) http://sub2.sub1.example.com/$1 [R=303,L]

A 303 redirect is similar to a 301 or 302, but conveys no information about the duration of the redirect; It does not say the URL change is permanent or temporary, it just says, "see this other URL."

Replace the broken pipe "Š" character in the pattern above with a solid pipe before use; Posting on this forum modifies the pipe characters.

Jim

lokeshshettyk

9:07 am on Nov 19, 2008 (gmt 0)

10+ Year Member



Thanks a lot guys. That fixed my problem. You have been very helpful.

Cheers :)