Forum Moderators: phranque
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?
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
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?
RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^m\.example\.com [NC]
RewriteCond %{QUERY_STRING} !&?mob=1&? [NC]
RewriteRule ^/(.*)$ /m/$1?mob=1 [NC,QSA,L]
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