HTACCESS Rewrite Condition and Rule
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I need help creating a rewrite condition and rewrite rule for the following scenarios, please.

The rule will begin with...

RewriteEngine on

RewriteCond %{HTTP_USER_AGENT} iPhone

  1. Check if HTTP Referer is NOT m.example.com or www.example.com (including anything after the root)
  2. If the the user passes the condition above, then rewrite URL to the ROOT of the mobile subdomain - http://m.example.com

Thank you!

awarded to Wikimedia

Crowdsource coding tasks.

2 Solutions


that should work

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} iPhone
RewriteCond %{HTTP_REFERER} !^http://(m|www).example.com
RewriteRule ^(.*)example.com/(.*) http://m.example.com/$2 [L,QSA]
Could you please explain the last line how it works?
jlevey almost 6 years ago
1) if user agent is iphone 2) if referer is not (m|www).example.com 3) take the url (first wildcard to deal with different subdomains) and then everything after root (second wildcard) redirect to mobile domain. IF your "rewrite URL to the ROOT of the mobile subdomain" mean that it must drop the rest of original url then remove $2 from last line. Explanation: [L,QSA] means: "L" - it is the last rule, "QSA" (Query String Append) if in orignal address there were some GET parameters then don't loose them and add them to redirected url
ochi almost 6 years ago
? any comment? please provide exact urls example of what you want to accomplish
ochi almost 6 years ago
I tried the original version and no redirect happened. Then I tried it with the RewriteRule line being RewriteRule (.*) http://m.example.com/$2 [L,R] but this only redirected the homepage of the desktop site to the mobile homepage.
jlevey almost 6 years ago
cause you've got only $1 variable so it should be written as RewriteRule (.*) http://m.example.com/$1 [L,R]
ochi almost 6 years ago
Thank you. For example, I want the homepage and any internal landing page (i.e. www.example.com/about-us) to redirect to m.example.com
jlevey almost 6 years ago
www.example.com/about-us must go into m.example.com/about-us ? or JUST to m.example.com without "about-us" part?
ochi almost 6 years ago
I would like the mobile homepage to be the catch-all please. We don't have a full-scale mobile site, but everything the visitor would need is on the mobile landing page. I have a view full site link on the mobile homepage so that's why I have to ignore traffic referred from the domain.
jlevey almost 6 years ago

I think this should work

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} iPhone
RewriteCond %{HTTP_REFERER} !^http://(m|www).example.com
RewriteRule (.*) http://m.example.com/ [L,QSA]
Our HTACCESS file is on an IIS server so it's not "playing nice"
jlevey almost 6 years ago
Try replacing RewriteRule (.*) http://m.example.com/ [L,QSA] with RewriteRule ^(.*)example.com http://m.example.com/ [L,QSA] And if it doesn't work try replacing RewriteCond %{HTTP_REFERER} !^http://(m|www).example.com with RewriteCond %{HTTP_HOST} !^http://(m|www)\.example\.com too. Sincerely, I'm not very friendly with .htaccess so I might fail several times
alv-c almost 6 years ago
The site is in ColdFusion and we're emulating an Apache server with an HTACCESS file. Nothing has worked. Sorry.
jlevey almost 6 years ago
View Timeline