Bainn
Bainn

Reputation: 43

.htaccess add tablet redirect

Using the .htaccess file, I wish to redirect tablet traffic to a tablet optimised site.

Regular URL: waxxxed.com.au Tablet Optimised URL: waxxxed.com.au/tablet/

I've tried the following code but it doesn't work.

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ipad [NC]
RewriteCond %{HTTP_HOST} !^ipad\. [NC]
RewriteRule ^ http://waxxxed.com.au/tablet/ [L,R=301]

x3 questions...

  1. This code doesn't work. Appears to create a loop. Not sure, but that may be related to my existing .htaccess code provided below.
  2. I want to redirect ALL tablets (not mobiles) not just iPads. How do I capture other tablet brands?
  3. Can you recommend any further optimisation of my .htaccess coding.

Full current .htaccess file (it works!)...

<IfModule mod_expires.c>
# Enable cache expirations
ExpiresActive On
# Default directive
ExpiresDefault "access plus 1 month"
# My favicon
ExpiresByType image/x-icon "access plus 1 year”
</IfModule>

# force redirect of html to no-extension
RewriteCond %{THE_REQUEST} ^GET\s.+\.html
RewriteRule ^(([^/]+/)*[^/.]+)\.html$ http://waxxxed.com.au/$1 [R=301,L]

# www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.waxxxed.com.au
RewriteRule (.*) http://waxxxed.com.au/$1 [R=301,L]

# Redirect from / to non-/
RewriteEngine On
RewriteRule ^(.*)/$ $1 [R=301,NC,L]

# parse file as file.html
RewriteCond %{REQUEST_FILENAME}\.html -f 
RewriteRule ^(([^/]+/)*[^/.]+)$ $1.html [L] 

# refer not found content to 404 page
ErrorDocument 404 /404page.html

Upvotes: 3

Views: 2098

Answers (1)

Jon Lin
Jon Lin

Reputation: 143896

The reason why it creates a loop is because you're checking that the hostname doesn't start with ipad, and since you're redirecting to waxxxed.com.au, it's never going to start with ipad. Instead check for the request URI starting with /tablet/:

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ipad [NC]
RewriteCond %{REQUEST_URI} !^/tablet/. [NC]
RewriteRule ^ http://waxxxed.com.au/tablet/ [L,R=301]

As for #2, you need to lookup user-agents for other tablets. In general, you want to find "android" but not "mobile":

RewriteCond %{HTTP_USER_AGENT} android [NC]
RewriteCond %{HTTP_USER_AGENT} !mobile [NC]
RewriteCond %{REQUEST_URI} !^/tablet/. [NC]
RewriteRule ^ http://waxxxed.com.au/tablet/ [L,R=301]

The rest of your htaccess file looks fine as long as it's doing what you expect, anything would be really nit-picky.

Upvotes: 2

Related Questions