Use .htaccess to change the url example.com/mobile to m.example.com

.htaccessrewrite

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

Best Answer

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]
Related Topic