5

I want this URL:

http://www.example.com/path/antyhing

to NOT be redirected.

Here's what I have which is not working:

RewriteCond %{REQUEST_URI} !^/path/.*$ [NC]
RewriteRule ^ http://m.example.com/ [R,L]

Currently it redirects all URLs to http://m.example.com/

Here is the full code in my .htaccess file:

RewriteBase /
RewriteEngine On

# Check if mobile=1 is set and set cookie 'mobile' equal to 1
RewriteCond %{QUERY_STRING} (^|&)mobile=1(&|$)
RewriteRule ^ - [CO=mobile:1:%{HTTP_HOST}]

# Check if mobile=0 is set and set cookie 'mobile' equal to 0
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
RewriteRule ^ - [CO=mobile:0:%{HTTP_HOST}]

# Skip next rule if mobile=0
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
RewriteRule ^ - [S=1]

# Check if this looks like a mobile device
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC,OR]
RewriteCond %{HTTP:Profile}       !^$

# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST}          !^m\.
# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP:Cookie}        !\mobile=0(;|$)
# Don't redirect "path" pages
RewriteCond %{REQUEST_URI} !^.+?/path/.*$ [NC]
# Now redirect to the mobile site
RewriteRule ^ http://m.example.com/ [R,L]
Community
  • 4,922
  • 7
  • 25
  • 37

1 Answers1

8

Can you replace your last rule with this:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# Check if mobile=1 is set and set cookie 'mobile' equal to 1
RewriteCond %{QUERY_STRING} (^|&)mobile=1(&|$)
RewriteRule ^ - [CO=mobile:1:%{HTTP_HOST}]

# Check if mobile=0 is set and set cookie 'mobile' equal to 0
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
RewriteRule ^ - [CO=mobile:0:%{HTTP_HOST}]

# Skip next rule if mobile=0 [OR] if it's a file [OR] if /path/
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$) [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_URI} ^.*/path/.*$
RewriteRule ^ - [S=1]

# Check if this looks like a mobile device
RewriteCond %{HTTP_PROFILE}       !^$ [OR]
RewriteCond %{HTTP_X_WAP_PROFILE} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST}          !^m\.
# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE}        !mobile=0(;|$)
# Don't redirect "path" pages
RewriteCond %{REQUEST_URI} !^.*/path/.*$ [NC]
# Now redirect to the mobile site
RewriteRule ^ http://m.example.com/ [R,L,NC]

Edit by OP: The only problem was coming from the %{REQUEST_URI} that for a reason that I don't understand only works against ^.*/path/.*$

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    Thank you for your answer, I just did but it still redirect all pages :( What could be wrong? – Community Sep 18 '13 at 12:22
  • It is indeed super strange. Did you test my entire code and it works for you? If yes, then what could wrong in my side? I don't have other `.htaccess` file, could it be browser caching? I'm trying to understand and solve this issue today... Kind of desperate after spending 3 hours trying every possible combination! – Community Sep 18 '13 at 12:37
  • Thank you so much! My Apache version is 2.2.22 running on CentOS 5.8, it's a DV Managed server on MediaTemple, thanks! – Community Sep 18 '13 at 13:23
  • This is to detect User-Agent-Profile in the HTTP request header, which is sent by some Mobile Devices (see http://en.wikipedia.org/wiki/UAProf). If it can help, the original code in my `.htaccess` comes from this answer: http://stackoverflow.com/a/7081855/2327283 – Community Sep 18 '13 at 16:57
  • It's working for you? It doesn't for me!!! Can you post the full code that works for you? I don't understand what I'm doing wrong! :( – Community Sep 18 '13 at 17:18
  • I think there is an error in your code, the 3rd line after "`# Check if this looks like a mobile device`" should not have `[NC,OR]` (remove the `OR`), no? – Community Sep 18 '13 at 17:33
  • I tried your code in a clean empty `.htaccess` file and your code does not work. It does not detect mobile devices correctly (I'm being redirected even from a desktop browser) and /path/mypage/ is also redirected. I'm getting crazy! – Community Sep 18 '13 at 17:39
  • Forget testing from desktop. Just test from mobile device using the regular URL http://www.example.com/ which should redirect to http://m.example.com/ except for http://www.example.com/path/no-redirect/. _By the way you can simulate mobile testing in Google Chrome:_ **F12 > Settings (bottom right) > Overrides > User Agent > iPhone** – Community Sep 18 '13 at 17:50
  • 1
    Yes thanks for your time... I'll keep looking... Gave you +1 for the time spent ;) – Community Sep 18 '13 at 19:06
  • Thanks I tried that and it still redirects my /path/ pages... :( I tried again to copy/past your entire code in a clean .htaccess file and it doesn't work... I also tried to restart Apache... Maybe there's something wrong with the server configuration? – Community Sep 18 '13 at 19:45
  • Of course I have, otherwise the redirection would not work. The problem is that I want to avoid the redirection for /path/ – Community Sep 18 '13 at 20:07
  • 2
    You won't believe I finally made it work!!! I have no idea why, but the following works: `RewriteCond %{REQUEST_URI} !^.*/path/.*$` – If I remove the `.*` at the beginning it won't work! I will update your answer so I can accept it ;) Cheers! – Community Sep 18 '13 at 21:56
  • No no `/path/` is the start, I'm not that stupid ;) `http://www.example.com/path/` !!! – Community Sep 18 '13 at 22:01
  • 1
    thanks so much! your solution worked perfectly for me :D my RewriteCond too, for some reason, worked only with `^.*` appended at the starting of path – Mr.Flocker Jun 09 '22 at 23:26