Solved

HTACCESS with ReWrite to redirect smartphones not working

Posted on 2013-06-17
9
492 Views
Last Modified: 2013-11-19
HI -
Using jQuery Mobile I designed a version of handyman-central.net for handheld devices and uploaded the files into "handyman-central.com/m".   Now I'm trying to get smartphones to be redirected to that "m" directory.  First I put a javascript in the full site's home page but that only worked for iPhones.  Now I'm trying htaccess file in the root directory and I have questions because it doesn't seem to work.

Can some expert please take a look at this and help me correct this code?

RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^handyman-central.net/index.html$ http://handyman-central.net/m/index.html [R,L]

Open in new window


Thanks.
d2
0
Comment
Question by:dzash2000
  • 4
  • 4
9 Comments
 
LVL 2

Accepted Solution

by:
laurent_roy earned 500 total points
ID: 39255737
Hello,

try that (remove the domain name in the first part of the rewrite rule) :

RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^index.html$ http://handyman-central.net/m/index.html [R,L]

Open in new window

see "what is matched?" in http://httpd.apache.org/docs/current/en/mod/mod_rewrite.html#rewriterule

I suppose you are in Directory context. If you change your domain name, it won't work anymore.

So try that :
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^index.html$ m/index.html [R,L]

Open in new window

0
 

Author Comment

by:dzash2000
ID: 39256583
laurent_roy -
Thank you for the response.
When I tried your first code block there was no change.
When I tried the second I got a server error.
I read the Apache module documentation but it is not sinking in. I apologize for being so thick.

I hope you have some other suggestions.

Best -
D2
0
 
LVL 2

Expert Comment

by:laurent_roy
ID: 39256785
Have you other rules ? (I tried both, it works for me)
0
 

Author Comment

by:dzash2000
ID: 39257026
If I understand your question - there is nothing else on the htaccess but this.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:dzash2000
ID: 39257107
I wound up with this working with iPhone5 (I'd appreciate tests from other smartphones) :
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^http://handyman-central.net$ http://handyman-central.net/m [R,L]

Open in new window


If you know would you please answer some really silly, basic questions about scripts for Apache:
I know htaccess should be plain text.  Should the code also be without any line breaks?
What is the meaning of the ^ after RewriteRule and the $ after .net?
Should the code be without any spaces ?  Such as between [NC] and RewriteRule?

Thanks in advance,
D2
0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 39257676
mod_rewrite documentation
Introduction to RegEx in Apache
.htaccess files

.htaccess files are plain text, and white-space can matter, depending on the context.  For example, many directives use white-space as the delimiter for its parameter list.  With RewriteRule, the syntax is:
RewriteRule Pattern Substitution [flags]

Open in new window

Including a space or line-break in the middle of the Pattern or Substitution could obviously lead to issues.  However, the spaces between parameters absolutely need to be there, and should be styled to assist readability.

In regular expressions, the caret (^) indicates the beginning of a line.  The dollar-sign ($) indicates the end of one.  They are used as hard-delimiters to ensure you are comparing the entire contents of an input, not just matching a substring.  For example, without the $ at the end, your rule could also match "http://handyman-central.net.com"

While you've closed this question, your rule is technically incorrect, and I have no idea why it would work as written.  RewriteRule does not compare the protocol/host/port section of the requested URL - it receives only the path and filename.  Your rule should look like this:
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{SCRIPT_FILENAME} !^/?m/.* [NC]
RewriteRule ^/?(.*)$ http://handyman-central.net/m/$1 [R,L]

Open in new window

The additional RewriteCond directive is to prevent an infinite redirection loop.

One final note: you should not have to write specific rules for every device, but rather every UA capable of running on those devices.  The browser application determines the user-agent string it sends in the request, not the actual hardware.  You can find compiled lists of these strings very easily on Google.  Here is one example.
0
 
LVL 2

Expert Comment

by:laurent_roy
ID: 39257848
It works with line breaks, but because you can have several blocks of rewritecond/rewriterule, I prefer not to have line break between the rewritecond and the rewriterule, but have line break after the rewriterule and before the next rewritecond.

First, a rewriterule is written like that :
RewriteRule pattern target

The pattern can't contain the domain name, simply because if you write a rewrite rule, it's mean you are on your web server.

So you can't write :
RewriteRule http://handyman-central.net ...

But you can write :
RewriteRule index.html ...
It means that everything that contains index.html will be rewrite to ...
"everything that contains" is very important regarding your question.

If you request fooindex.html, it will be rewrite.

But if you write :
RewriteRule ^index.html$ ...
It means that only index.html will be rewrite, because ^ means the beginning of the URL and $ means the end.

^index.html means every that starts with index.html, and index.html$ means everything that ends with index.html

If you want to rewrite everything (when the rewritecond matches), you must use
  RewriteRule ^(.*)$ /m/$1 [R,L]

.* means everything, and $1 means what is inside () in the pattern.
So it means that eveything will be redirect to /m/everything.

So to sum up, your solution is :

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

Open in new window


RewriteCond %{REQUEST_URI} !^/m/     is needed to avoid loops : ! means not, so only what is not beginning with /m/ matches this condition

But I suggest you to start with something simple, like RewriteRule index.html /m/index.html [R,L]

If it doesn't work, there's no need to try something else.

perhaps I'd better begin with that : have you enabled Rewrite ?
0
 

Author Comment

by:dzash2000
ID: 39259553
Thanks so much for the great information.

If I understand correctly "Rewrite" is enabled by the system admin.  I communicated with Godaddy about what I was attempting and they did not specifically say that Rewrite was NOT enabled.  In fact they said that any changes I made should take effect immediately.

If Rewrite is something that should be enabled on the htaccess file -you should know that I'm only using the lines I have shown you and the lines you have provided to me.

I will be trying your sum up solution shortly but wanted to thank you now for the excellent follow up.
D2
0
 
LVL 2

Expert Comment

by:laurent_roy
ID: 39259946
you need to add "RewriteEngine On"

try that
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
  RewriteCond %{REQUEST_URI} !^/m/
  RewriteRule ^(.*)$ /m/$1 [R,L]
</IfModule>

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This video teaches users how to migrate an existing Wordpress website to a new domain.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now