Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Trying To Redirect Users To Mobile Site

cant get httpd.conf or .htaccess to work!



6:14 pm on May 29, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member


I am trying to pass people to my mobile site based on User Agent. I have tried a variety of codes, both in htaccess and apache httpd.conf. none of them seem to work for me.

I used this in .htaccess:

RewriteEngine On
# if the browser accepts these mime-types, it's definitely mobile, or pretending to be
RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC,OR]

# a bunch of user agent tests
RewriteCond %{HTTP_USER_AGENT} "sony|symbian|nokia|samsung|mobile|windows ce|epoc|opera" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "mini|nitro|j2me|midp-|cldc-|netfront|mot|up\.browser|up\.link|audiovox"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "blackberry|ericsson,|panasonic|philips|sanyo|sharp|sie-"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "portalmmm|blazer|avantgo|danger|palm|series60|palmsource|pocketpc"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "smartphone|rover|ipaq|au-mic,|alcatel|ericy|vodafone\/|
lge\ |maemo|midp|mmp|opera\ m(ob|in)i|palm(\ os)?|phone|p(ixi|re)\/|plucker|pocket|psp|
symbian|treo|up\.(browser|link)|vodafone|wap|windows\ (ce|phone)|xda|xiino" [NC]

# rewrite rules here
RewriteRule .? http://m.SITE.com%{REQUEST_URI} [L,R=302]

And Nothing when testing from my iPhone.

Here is what I have in my httpd.conf:

RewriteBase /

RewriteCond %{HTTP_USER_AGENT} android|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|
ip(hone|od)|iris|kindle|lge\ |maemo|midp|mmp|opera\ m(ob|in)i|palm(\ os)?|phone|p(ixi|re)\/|
plucker|pocket|psp|symbian|treo|up\.(browser|link)|vodafone|wap|windows\ (ce|phone)|xda|xiino [NC]

# RewriteCond %{HTTP_USER_AGENT} ^(1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a\ wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|
an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r\ |s\ )|avan|be(ck|ll|nq)|bi(lb|rd)|
esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1\ u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|
haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp(\ i|ip)|hs\-c|ht(c(\-|\ |_|a|g|p|s|t)|tp)|
hu(aw|tc)|i\-(20|go|ma)|i230|iac(\ |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|
jbro|jemu|jigs|kddi|keji|kgt(\ |\/)|klon|kpt\ |kwc\-|kyo(c|k)|le(no|xi)|lg(\ g|\/(k|l|u)|50|54|e\-|
mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-|\ |o|v)|zz)|mt(50|p1|v\ )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|
sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v\ )|sy(01|mb)|
w3c(\-|\ )|webc|whit|wi(g\ |nc|nw)|wmlb|wonu|x700|xda(\-|2|g)|yas\-|your|zeto|zte\-) [NC]

RewriteRule ^$ http://m.SITE.com [R,L]

And no go on this either. And yes, I have restarted the server!

I am at wits end- these look good to me!

And I checked in my access log- I AM passing the user agent:

108.123.456.789 - - [29/May/2014:12:03:37 -0600] "GET /cartoon_characters/URL.html HTTP/1.1" 200 6973 "http://www.SITE.com/cartoon/URL.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D201 Safari/9537.53"

Thanks for your ideas on getting this working!


[edited by: phranque at 11:29 am (utc) on May 31, 2014]
[edit reason] unlinked urls in code [/edit]


7:44 pm on May 29, 2014 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

Wait, wait, one thing at a time. You can't dump the same rules in both htaccess and config; mod_rewrite isn't inherited the way other Apache mods are. Where are your current, existing, successful RewriteRules located?

Rules in htaccess don't require a server restart, so they're useful for testing. Did you remember to set the appropriate AllowOverrides directive?

This rule
RewriteRule ^$ http://m.example.com [R,L] 

will only work on requests for the root, and then only if it's in either htaccess or the appropriate <Directory> section of the config file. And if the rule did work, you'd get an infinite loop unless you add a condition to exclude requests for the mobile site.

Tangential note: Try to avoid [NC] unless you're doing an all-encompassing lockout. It may look trivial, because it's just two more bytes when you're typing the rule, but it makes a lot more work for the server. A legitimate UA comes with a specific casing (like "Googlebot" vs. fake "GoogleBot"); use it.

The [R] flag creates a 302 redirect. That's not what you want is it?


8:33 pm on May 29, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

Sorry- I was not clear. I tried BOTH.... first the .htaccess, then (erasing that!), the apache rules. I only restarted for the httpd.conf but I doubt it would hurt for the .htaccess...

This is the overrides from httpd.conf:

<Directory "/hd2/web/S/SITE/public_html">
AllowOverride None
Options ExecCGI FollowSymLinks

ANd the code followed. At this point, I just wanted it to work for root. Once I get that working, I will add a (.*) & $1 to forward ALL pages.... and then a cookie to OVERRIDE the push to the mobile site if requested.

Thanks for the NC advice. Will change that.

RE the 302 flag... honestly, I just copied that over and wanted to see it work. I had not actually gotten to the clean up phase!



9:55 pm on May 29, 2014 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

But if you don't allow overrides, how can the htaccess work?


11:21 pm on May 29, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

Is that what that does? :)

So drop that... let me try...

Nope, still not hitting the mobile site


1:44 am on May 30, 2014 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

Is that what that does?

Oh, lord.

:: shuffling papers ::


(Auto-linking intentionally disabled so the fragment doesn't get lost.)
That's for 2.2. In 2.4 -- just change one digit in the URL -- there's a slightly crucial difference: the default has changed from "All" to "None". Either way
AllowOverride FileInfo
will permit mod_rewrite directives. (There are further 2.2/2.4 variations involving Options, but those need not concern us.)

Did you restart after changing the AllowOverride line? Once you've done this, any future htaccess activity will be recognized and you don't need to keep restarting. If you're making your changes in the config file, you will of course have to restart every time. That's assuming everything is happening in the appropriate directory.

Before messing with the details of the rule-- which is a whole nother issue-- make sure mod_rewrite is getting recognized. I like to make up something like

RewriteRule foobar http://www.example.com/widget.html [R=301,L]

Request anything with "foobar" in the path and I'll end up on the 404 page-- but the browser's address bar will say "widget.html" so I know the rule has worked. The idea is to make up a name that won't occur in a real request, so you can experiment without hurting anyone. (This is a live server, right?)


3:22 pm on May 30, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

Sorry I am late getting back....

OK, I added the code you sent EXACTLY into my .htaccess

I get a page that says EXAMPLE DOMAIN- This domain is established to be used for illustrative examples in documents. You may use this domain in examples without prior coordination or asking for permission.

So that means the .htaccess is working.

So I commented out all the forward code and broke it down to the simplest form:

RewriteEngine On
RewriteRule foobar http://www.example.com/widget.html [R=301,L]

RewriteCond %{HTTP_USER_AGENT} "iPhone" [NC]
RewriteRule .? [m.MYDOMAIN.com%{REQUEST_URI}...] [L]

And I put that at the TOP of the .htaccess

No forward....


Also, FWIW, this is Apache 1.3...


6:18 pm on May 30, 2014 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

1.3 ?!?!?!

Is this your own server (that is, one that physically belongs to you), or do you have a very, very conservative host?

So that means the .htaccess is working.

No, it just means the domain exists.

Try one more quick thing: Introduce an error into the htaccess. Throw in a comma, let your cat walk across the keyboard, whatever. Go to the site. Do you get a 500 error?


12:04 am on Jun 1, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

It is my own host. It is stable at 1.3 and does all I need, I see no reason to upgrade it. Besides, I have written a few Mod_Perl plug ins that I use in 1.3 that I tried to convert to 2.2 and they never worked out, so I just stayed there....

I added some errant letters to .htacces... instant 500 error, yes.

Featured Threads

Hot Threads This Week

Hot Threads This Month