我正在设置一个社交网络脚本,它有桌面版和移动版网站。当移动设备尝试浏览该网站时,移动页面将使用网址 www.example.com/mobile 提供。我想要一个 .htaccess 重写规则,可以更改所有www.example.com/mobile网址m.example.com无需真正重定向到子域 m 这是我能想到的
RewriteCond %{HTTP_HOST} ^m.example.com RewriteRule (.*) http://www.example.com/mobile/$1 [R=301,L]
但是我收到了一个错误...Rewitecond: Bad flag delimiters
我该如何解决这个问题或者有什么更好的方法吗?
PS 我没有 .htaccess 文件,我直接在 httpd.conf 指令中设置这些规则。我认为这不会有什么不同,但我认为最好提一下……以防万一。
此后,我已将启用的 .htaccess 移出,并从 httpd.conf 文件中取出重写规则,并测试 .htaccess 是否有效以及 mod_rewrite 是否已启用。我使用此代码来验证 mod_rewrite 确实已启用
<?php
ob_start();
phpinfo(INFO_MODULES);
$contents = ob_get_contents();
ob_end_clean();
var_dump(strpos($contents, 'mod_rewrite') !== false);
?>
当我运行此文件时,我得到一个输出bool(true)
另外我注意到像下面这样的重写规则工作正常
# 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]
但这些规则不起作用
RewriteRule ^link([^/]*).html$ rewrite.php?link=$1 [L]
RewriteCond %{HTTP_HOST} ^m.jukpac.com
RewriteRule (.*) http://www.jukpac.com/mobile/$1 [R=301,L]
为什么我的重写规则仅部分起作用?:-/
以下是重写日志的快照
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
答案1
看起来你得到了一个解析错误,因为 RewriteCond 语句试图解释[R=301,L]
,尽管那个语句是用于的RewriteRule
。
尝试将它们放在不同的行上,如下所示:
RewriteCond %{HTTP_HOST} ^m.example.com
RewriteRule (.*) http://www.example.com/mobile/$1 [R=301,L]