Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to exclude a URL from htaccess rule

I'm trying to setup a global mobile redirect in my httpd.conf for all of my sites on the server and only have to edit it once, vs. 92 times. All sites mobile will redirect to one locate so that's fine.

However I want to exclude one particular URL from redirecting at all. Here's what I have so far that isn't working and causing a redirect loop:

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^.*iphone|ipod|blackberry|android|sgh|sonyericsson|psp|mot|htc|lg|nokia|palm|treo|j2me|webos|smartphone|symbian.*$ [NC]
RewriteCond %{REQUEST_URI} !^http://www.example.com/mobile [NC]
RewriteRule ^(.*) http://www.example.com/mobile [R=302,L]

Can anyone figure out why it's causing a redirect loop on that particular url? It's just a folder on that domain with a .htaccess in it only containing RewriteEngine off and an index.php file with a header() redirect call.

like image 499
Logan Best Avatar asked Nov 08 '12 04:11

Logan Best


1 Answers

The %{REQUEST_URI} variable will never look like http://www.example.com/mobile, because it will never contain protocol (the "http://" bit) or host information (the "www.example.com"` part). You only want the URI part:

RewriteCond %{REQUEST_URI} !^/mobile 

If you need to check the hostname as well, you need an additional condition:

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
like image 81
Jon Lin Avatar answered Sep 23 '22 16:09

Jon Lin