Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm a former C++ programmer now employed as a manager and using my skills to limp along in web design, etc. Our site is running pretty well but I'm really stumbling over some Apache rewrite code to redirect to our mobile website.

We want to redirect both www.domain.org and domain.org to m.domain.org (the hostless version is needed for links from Google and Bing.) We also have a "full site" button on the mobile page that sends folks to www.domain.org. Here's the code:

# prevent looping
RewriteCond %{HTTP_HOST} !^m\.

# a bunch of user agent tests
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC,OR]
RewriteCond %{HTTP:Profile}       !^$

# rewrite rules here
RewriteRule ^(.+)\$ http://m.animalhealthassociates.org/$1 [R=302,NC] // This does not redirect animalhealthassociates.org
# RewriteRule ^ http://m.animalhealthassociates.org/ [R=302,NC] // This redirects the above but also redirects the Full Site link


I will freely admit I don't know much about regular expressions, and my current situation does not allow me to take the time to gain the knowledge to fully understand this. What on earth do I need to do to meet both requirements?
Posted

I think the problem in your rule is the \$ matching a literal "$" sign, when it should be just $ to match the end of the string.

Take a look at the URL rewriting guide[^] for some decent examples.
 
Share this answer
 
Thx Graham - the problem was at the other end. Somehow I lost the line that was supposed to prevent the Full Site link from redirecting:

C#
RewriteCond %{HTTP_REFERER} !^http://(.*).animalhealthassociates.org/.*$ [NC]


But in the process of puzzling this all out I now understand the regex stuff a bit better, though still far from expert (or even competent actually.)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900