Mobile Redirect using htaccess – Stack Overflow

Mobile Redirect using htaccess - Stack Overflow

Get via App Store Read this post in our app!

Mobile Redirect using htaccess

I have a website called

I have a mobile website called

I want to use an htaccess to automatically redirect the main website URL to the mobile version..

However, there is a link on the mobile version that points back to the main website called

When I click the logo on the home page of the actual website it links to

I don’t want the user to be permitted back to mobile accidentally by clicking on the logo on the main page. How can I accomplish this via htaccess without JavaSCript.

If not I am open-minded to alternate options.

I think I am presently going to use this for sensing mobile redirect via htaccess

9 Answers

I tested bits and chunks of the following, but not the accomplish rule set in its entirety, so if you run into trouble with it let me know and I’ll dig around a bit more. However, assuming I got everything correct, you could attempt something like the following:

Tim Stone’s solution is on the right track, but his initial rewriterule and and his cookie name in the final condition are different, and you can not write and read a cookie in the same request.

Here is the finalized working code:

I modified Tim Stone’s solution even further. This permits the cookie to be in Two states, 1 for mobile and 0 for utter. When the mobile cookie is set to 0 even a mobile browser will go to the utter site.

Here is the code:

Thanks Tim Stone, naunu, and Kevin Bond, those answers indeed helped me. Here is my adaption of your code. I added the functionality to be redirected back to the desktop site from m.example.com in case the user does not visit the site with a mobile device. Additionally I added an environment variable to preserve http/https requests:

This seems to work fine except one thing: When I’m on the desktop site with a desktop device and I visit m.example.com/?m=1, I’m redirected to example.com. When I attempt again, I “stay” at m.example.com. It seems as if the cookie isn’t set and/or read correctly the very first time.

Maybe there is a better way to determine if the device is a desktop device, I just negated the device detection from above.

And I’m wondering if this way all mobile devices are detected. In Tim Stone’s and naunu’s code that part is much larger.

Similarly, if you desired to redirect to a sub-folder instead of a sub-domain, do the following:

Working off of Kevin’s good solution you can add this to the .htaccess file in your site’s root directory:

Then, in the /m/ folder, add or create an .htaccess with the following:

I know it’s not a direct response to the question, but somebody (like me) might stumble upon this question and wonder how that method would be accomplished as well.

For Mobiles like domain.com/m/

You can also attempt this. Credits to the original author who has since eliminated the script

Just edit the $mobile = “http://www.stepforth.mobi”,

Very first, go to the following URL and download the mobile_detect.php file:

Insert the following code on your index or home page:

Leave a Reply

Your email address will not be published. Required fields are marked *