Mike Rifgin
Mike Rifgin

Reputation: 10761

htaccess and forward one url to another

I have this rewrite code:

RewriteCond %{HTTP_USER_AGENT} ^.*iPhone.*$
RewriteRule ^(.*)$ http://stagingsite.com/site/mobile [R=301]
RewriteRule ^faq/$ /mobile/faq

The first line is working correctly. If the user is on an iphone then redirect to the mobile directory where the index page is displayed.

I also want users visiting:

http://stagingsite.com/site/faq

to get forwarded to http://stagingsite.com/site/mobile/faq if they're on an iphone but the last line of code above doesn't seem to be achieving this.

Any ideas what I have wrong?

Upvotes: 0

Views: 41

Answers (2)

Jon Lin
Jon Lin

Reputation: 143966

RewriteCond directives only get applied to the *immediately following RewriteRule. So you have the condition that checks for iPhone, but that only gets applied to the redirect rule, and not the faq rule. You have to duplicate it:

RewriteCond %{HTTP_USER_AGENT} ^.*iPhone.*$
RewriteRule ^(.*)$ http://stagingsite.com/site/mobile [R=301,L]

RewriteCond %{HTTP_USER_AGENT} ^.*iPhone.*$
RewriteRule ^faq/?$ /site/mobile/faq [L]

You should also include L flags so the 2 rules don't accidentally interfere with each other. And your regex and target needs to be updated to accept an optional trailing slash and the subdirectory.

Upvotes: 1

Santiago Rebella
Santiago Rebella

Reputation: 2449

taking out the slash before mobile as

RewriteRule ^faq/$ mobile/faq 

works?

Upvotes: 0

Related Questions