0

I have am setting up a social network script, it has both a desktop and a mobile version of the site. When a mobile device tries to browse the site, the mobile page is served with the url www.example.com/mobile I want a .htaccess rewrite rule that can change all the www.example.com/mobile url to m.example.com without actually redirecting to the subdomain m This is what I could come up with

RewriteCond %{HTTP_HOST} ^m.example.com RewriteRule (.*) http://www.example.com/mobile/$1 [R=301,L]

But i am getting an error... Rewitecond: Bad flag delimiters How do I fix this or what would be a better way to do this?

P.S I dont have a .htaccess file I am setting up these rules directly inside the httpd.conf directive. I don't think that is going to make any difference but just thought it would be better to mention... just in case.

I have since moved enabled .htaccess and taken the rewrite rules out of the httpd.conf file and tested if .htaccess works and also that mod_rewrite is enabled. I am using this code to verify that mod_rewrite is indeed enabled

<?php
ob_start();
phpinfo(INFO_MODULES);
$contents = ob_get_contents();
ob_end_clean();
var_dump(strpos($contents, 'mod_rewrite') !== false);
?>

When i run this file i get an output bool(true) Also I have noticed rewrite rules like the one below is working fine

# 301 redirect if missing www.
    RewriteCond %{HTTP_HOST} !^$
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTPS}s ^on(s)|
    RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

But rules these rules won't work

RewriteRule ^link([^/]*).html$ rewrite.php?link=$1 [L]
RewriteCond %{HTTP_HOST} ^m.jukpac.com 
RewriteRule (.*) http://www.jukpac.com/mobile/$1 [R=301,L]

Why are my rewrite rules only partially working? :-/

Below is a a snap of the rewrite logs

173.245.62.183 - - [25/Jan/2014:02:58:42 --0500] [www.jukpac.com/sid#7fd2a13364a0][rid#7fd2a160a228/initial] (2) init rewrite engine with requested uri /rewrite.php
173.245.62.183 - - [25/Jan/2014:02:58:42 --0500] [www.jukpac.com/sid#7fd2a13364a0][rid#7fd2a160a228/initial] (1) pass through /rewrite.php
173.245.62.183 - - [25/Jan/2014:02:58:42 --0500] [www.jukpac.com/sid#7fd2a13364a0][rid#7fd2a160a228/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/rewrite.php -> rewrite.php
173.245.62.183 - - [25/Jan/2014:02:58:42 --0500] [www.jukpac.com/sid#7fd2a13364a0][rid#7fd2a160a228/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/rewrite.php -> rewrite.php
173.245.62.183 - - [25/Jan/2014:02:58:42 --0500] [www.jukpac.com/sid#7fd2a13364a0][rid#7fd2a160a228/initial] (3) [perdir /var/www/html/] applying pattern '^' to uri 'rewrite.php'
173.245.62.183 - - [25/Jan/2014:02:58:42 --0500] [www.jukpac.com/sid#7fd2a13364a0][rid#7fd2a160a228/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/rewrite.php -> rewrite.php
173.245.62.183 - - [25/Jan/2014:02:58:42 --0500] [www.jukpac.com/sid#7fd2a13364a0][rid#7fd2a160a228/initial] (3) [perdir /var/www/html/] applying pattern '[^/]$' to uri 'rewrite.php'
173.245.62.183 - - [25/Jan/2014:02:58:42 --0500] [www.jukpac.com/sid#7fd2a13364a0][rid#7fd2a160a228/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/rewrite.php -> rewrite.php
173.245.62.183 - - [25/Jan/2014:02:58:42 --0500] [www.jukpac.com/sid#7fd2a13364a0][rid#7fd2a160a228/initial] (3) [perdir /var/www/html/] applying pattern '^link([^/]*).html$' to uri 'rewrite.php'
173.245.62.183 - - [25/Jan/2014:02:58:42 --0500] [www.jukpac.com/sid#7fd2a13364a0][rid#7fd2a160a228/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/rewrite.php -> rewrite.php
173.245.62.183 - - [25/Jan/2014:02:58:42 --0500] [www.jukpac.com/sid#7fd2a13364a0][rid#7fd2a160a228/initial] (3) [perdir /var/www/html/] applying pattern '(.*)' to uri 'rewrite.php'
173.245.62.183 - - [25/Jan/2014:02:58:42 --0500] [www.jukpac.com/sid#7fd2a13364a0][rid#7fd2a160a228/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/rewrite.php -> rewrite.php
173.245.62.183 - - [25/Jan/2014:02:58:42 --0500] [www.jukpac.com/sid#7fd2a13364a0][rid#7fd2a160a228/initial] (3) [perdir /var/www/html/] applying pattern '^(.*)' to uri 'rewrite.php'
173.245.62.183 - - [25/Jan/2014:02:58:42 --0500] [www.jukpac.com/sid#7fd2a13364a0][rid#7fd2a160a228/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/rewrite.php -> rewrite.php
173.245.62.183 - - [25/Jan/2014:02:58:42 --0500] [www.jukpac.com/sid#7fd2a13364a0][rid#7fd2a160a228/initial] (3) [perdir /var/www/html/] applying pattern '^file/pic/photo/(.*)\\.(.*)$' to uri 'rewrite.php'
173.245.62.183 - - [25/Jan/2014:02:58:42 --0500] [www.jukpac.com/sid#7fd2a13364a0][rid#7fd2a160a228/initial] (1) [perdir /var/www/html/] pass through /var/www/html/rewrite.php
108.162.222.177 - - [25/Jan/2014:02:58:42 --0500] [www.jukpac.com/sid#7fd2a13364a0][rid#7fd2a15edb58/initial] (2) init rewrite engine with requested uri /static/ajax.php
108.162.222.177 - - [25/Jan/2014:02:58:42 --0500] [www.jukpac.com/sid#7fd2a13364a0][rid#7fd2a15edb58/initial] (1) pass through /static/ajax.php
108.162.222.177 - - [25/Jan/2014:02:58:42 --0500] [www.jukpac.com/sid#7fd2a13364a0][rid#7fd2a15edb58/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/static/ajax.php -> static/ajax.php
2
  • You've got nothing in your logs attached showing where you try these rules which does not work.
    – Petter H
    Jan 25, 2014 at 8:25
  • I am discarding this question and this silly error, tired sick of it moving to nginx based server at the very least I can understand what is going wrong in it with nginx
    – Rick Roy
    Jan 25, 2014 at 9:14

1 Answer 1

1

It looks like you get a parse error because RewriteCond statements seeks to interpret [R=301,L], although that one is intended for the RewriteRule.

Try putting them on different lines like this:

 RewriteCond %{HTTP_HOST} ^m.example.com     
 RewriteRule (.*)    http://www.example.com/mobile/$1 [R=301,L]
5
  • your solution fixed the error but I didn't get the required results. www.example.com/mobile/ doesn't change to m.example.com plz help me write the rules that you think can achieve what I want
    – Rick Roy
    Jan 15, 2014 at 20:04
  • well, this example should redirect the m.example.com, so try enabling some logging httpd.apache.org/docs/current/mod/mod_rewrite.html and check your logs. It will say what the value of HTTP_HOST is while evaluating each request.
    – Petter H
    Jan 15, 2014 at 22:36
  • Mod rewrite is enabled I have tested it, tried to add the rewrite log getting a 500 .. Editing the question to show the codes I am using to verify that mod_rewrite is enabled please take a look
    – Rick Roy
    Jan 16, 2014 at 4:18
  • you need to enable rewrite logging to determine where you fail. Error 500 is not related to rewrite rules.
    – Petter H
    Jan 16, 2014 at 17:53
  • take the php stuff out of the equation for now -- just monitor the mod rewrite logs and you're gonna find out where you fail for sure.
    – Petter H
    Jan 24, 2014 at 19:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .