flyboy
flyboy

Reputation: 11

Redirecting only a specific page request for mobile users using htaccess

I want to redirect requests for mobile users only to http://mydomain.com/video.html to http://mydomain.com/mobile/index.html using .htaccess

Currently it's a Wordpress site, so there is already some Wordpress stuff in my .htaccess file.

Here is what I have now:

AddHandler php5-script .php

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

# BEGIN Mobile redirect for the video 
<IfModule mod_rewrite.c>
RewriteEngine on

  # stuff to let through (ignore)
  RewriteCond %{REQUEST_URI} "/mobile/" 
  RewriteRule (.*) $1 [L]
  #
RewriteCond %{REQUEST_URI} !^/html/video.html.*$
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^(.*)$ /mobile/index.html [L,R=302]
</IFModule>
# END Mobile redirect

It is currently redirecting my iPad for all pages, and NOT for the page I want it to (video.html)

Am I missing something?

Upvotes: 1

Views: 3209

Answers (1)

LazyOne
LazyOne

Reputation: 165511

Try this one:

# BEGIN Mobile redirect for the video 
<IfModule mod_rewrite.c>
    RewriteEngine On
    # stuff to let through (ignore)
    RewriteRule ^mobile/ - [L]
    # redirect /video.html to /mobile/index.html for mobile browsers
    RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
    RewriteRule ^video\.html$ /mobile/index.html [L,R=302]
</IfModule>
# END Mobile redirect

1. Because you have WordPress it will be better to place this block BEFORE WordPress block.

2. There is no real need for # stuff to let through (ignore) rule if you have so little rewrite rules in this block (especially if you place whole block before WordPress one). But since I do not know for sure how your website works, I leave it for you to test.

3. I see you are using (.*) pattern in RewriteRule .. and then one extra pattern in RewriteCond to do actual URL matching. No need -- you can do all of that in RewriteRule itself.

For example, this:

RewriteCond %{REQUEST_URI} "/mobile/"
RewriteRule (.*) $1 [L]

can easily be replaced by

RewriteRule ^mobile/ - [L]

Upvotes: 3

Related Questions