Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I am using Apache's mod_rewrite to redirect mobile users to my mobile site based on their http_user_agent. However not all pages have a mobile equivalent. Also mobile pages end in .html and "full" pages end in .shtml.

Here is some pseudo code.

Does the user have a certain HTTP_USER_AGENT?

Is there a mobile page?

If so take them there. If not, no redirection is needed.

I want to do this with apache.

share|improve this question
    
You could do something with cURL and PHP, where an if-then statement determines whether or not to redirect using PHP's header('Location: http://newpage/') –  ionFish May 30 '12 at 16:22
    
I need to use apache –  ckliborn May 30 '12 at 18:34
    
Apache is what would serve the PHP, and PHP would use cURL. (cURL is built into PHP, and PHP renders the pages for Apache to serve.) –  ionFish May 30 '12 at 20:22
add comment

2 Answers 2

You CAN check for the existence of a resource identified by an HTTP URL using mod_rewrite

See example below:

RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{REQUEST_URI} ![.]html$
RewriteCond %{REQUEST_URI} ^([^.]*)
RewriteCond %1.html -U
RewriteRule ^.*$ $0.html [R=302, L] 

The import part is line 4 and the -U flag that checks if a resource exists. Note, that using this flag does impact performance on the web tier.

Other things to note. Firstly I have used a hack regex for user agent string to dtermine if the user agent is on a mobile device. This isn't always the best approach, depending on how extensible you want to make it. You might want to consider using the Apache Mobile Filter and the using a rewrite cond as follows (in place of the user agent cond):

RewriteCond %{ENV:AMF_DEVICE_IS_MOBILE} ^true

Finally, as DisgruntledGoat mentions, it is usually better to first use responsive web design and then only consider server side negotiation if you have a considerably different information architecture.

share|improve this answer
    
I can't get the "-U" option to work. It always seems to return 200. –  ckliborn Jun 19 '12 at 20:06
    
Also - what are you doing in line 3? –  ckliborn Jun 19 '12 at 20:07
    
A cheap way to get the path and filename without the extension –  William Greenly Jun 19 '12 at 21:46
add comment

This is not possible using htaccess alone. You would need to serve a PHP page that does the checking and redirection if the user agent is mobile. This might get very complex since if the mobile page does not exist you need to load the original .shtml file.

In this situation the best you can aim for is just redirecting users, then if the page doesn't exist, the 404 page can generate a link back to the original page (with a parameter stating not to redirect again).

However, you may be better off using other methods. Try looking into responsive web design, which allows you to serve the exact same file to different devices and have the content fit perfectly.

share|improve this answer
    
Not true - it can be done, see below –  William Greenly Jun 8 '12 at 15:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.