Forum Moderators: phranque
# 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 :)
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.
# 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]
Replace the broken pipe "Š" character in the pattern above with a solid pipe before use; Posting on this forum modifies the pipe characters.
Jim