Forum Moderators: phranque

Message Too Old, No Replies

Complicated rewrite rule

         

JoyceBabu

6:31 pm on Jun 20, 2009 (gmt 0)

10+ Year Member



Hello All,

I am trying to achieve something using mod_rewrite. Unfortunately, all times I am ending up with infinite redirect or no success. This is my situation.

Requirement
-----------
My site is at www.example.com. I am using mod_rewrite to redirect example.com to www.example.com. I want to create a mobile section of the site, which will be accessible from m.examples.com. But I want the subdomain to get its content from example.com/m/ transparently.

For example, m.example.com/robots.txt will return example.com/m/robots.txt. Also direct visit to example.com/m/robots.txt will 301 redirect to m.example.com/robots.txt.

This much I have achieved. Now comes the real problem. I have a news channel at example.com/news/. When someone visit the corresponding mobile version, ie m.example.com/news/, I want it to transparently rewrite to m.example.com/news/?mob=1 (Making my news section mobile enabled requires only one line change in the code. So I don't want to duplicate it and move it to example.com/m/news/).

My Result
---------
With all my codes, I am getting one of the following response
1. m.example.com/news/ rewrites to example.com/m/news/, which rewrite to m.example.com/news/ and so on. An infinite loop.
2. m.example.com/news/ rewrites to example.com/m/news/ which rewrites to example.com/news/ (without ?mob=1)

One Possible Solution
---------------------
But from what I understand, a possible solution would be to use something other than mod_rewrite for subdomain to sudirectory mapping.

Can somebody help me?

jdMorgan

7:15 pm on Jun 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could set up a separate virtual host for your mobile site, but that precludes easly getting your mobile news content from the main site.

You didn't post your code, so all I can do is ask: Did you explicitly prevent paths starting with /m/ and news requests which already have a query string of mob=1 appended from being re-rewritten by using a negative match RewriteCond on the two rules? If not, they'll likely loop.

Jim

JoyceBabu

9:24 am on Jun 21, 2009 (gmt 0)

10+ Year Member



Thanks for the virtual host idea. I will consider that too. If I can create a virtual host without using my Plesk cp, then I might be able to point it to main_site_root/m. I am not sure whether it will create any permission problem.

Using mod_rewrite, this is what I have finally come up with. This works only when I put it in my <VirtualHost> block.
-----------------
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(m\.) [NC]
RewriteCond %{QUERY_STRING} !(mob=1) [NC]
RewriteRule (.*) /m/%{REQUEST_URI}?mob=1 [QSA,NC,L]
-----------------

It works for all urls as expected, but not the homepage.
m.example.com/robots.txt returns example.com/m/robots.txt
m.example.com/news/ rewrites correctly to example.com/news/ with the ?mob=1 parameter and displays the mobile version. (This is done with another rewrite rule in /m/news/.htaccess)
m.example.com/m/ returns 404 error
www.example.com/m/ returns 404 error

Unfortunately, m.example.com/ returns example.com/

Any idea why it is not working for the homepage?

jdMorgan

1:27 pm on Jun 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This rule can be made much more robust and a bit more simple/efficient:

RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^m\.example\.com [NC]
RewriteCond %{QUERY_STRING} !&?mob=1&? [NC]
RewriteRule ^/(.*)$ /m/$1?mob=1 [NC,QSA,L]

This prevents, for example, mobile domain requests with queries with additional bogus or real query string name/value pairs of "mob=101" or "angry_mob=1" from unexpectedly failing to rewrite. It also rejects requests for m.this-site-is-junk.example.com (to avoid duplicate-content) by requiring the fully-qualified mobile domain in the request. Additional minor tweaks for efficiency.

However, this won't change the problem with rewriting of "/".

This failure of "/" rewriting is not likely caused by a flaw in this code -- either your original or this tweaked version. The only problem I see is that your original code will insert an additional slash into the filepath following "/m/" - e.g. "/m//abc.php". Apache will treat this identically to a request for "/m/abc.php", so it's wrong, but not the likely cause of the immediate problem.

The most likeley causes are:
1) A conflicting rule in the .htaccess file(s) along the directory path to your mobile content.
2) Interference from MultiViews or AcceptPathInfo (disable both unless you actually use them).

Jim

JoyceBabu

5:44 pm on Jun 22, 2009 (gmt 0)

10+ Year Member



Thanks a lot Jim. I followed your advice and created a separate virtual host and used the rewrite rule to access my parent site using symbolic link. Now everything is working fine. :)