关于使用apache mod_rewrite重写url的问题

时间:2009-12-01 14:39:42

标签: mod-rewrite

我怀疑使用apache mod_rewrite重写url。我是mod_rewrite的新手,我没有任何正则表达式的经验。

我想做的是:

重写 / / web / content / public /

重写 / clients / / web / content / clients /

我怎样才能实现上述目标。

我试过了:

   <IfModule mod_rewrite.c>

   Options +FollowSymLinks
   RewriteEngine on

   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-l

   RewriteRule    ^/clients/$ web/content/clients/ [L]
   RewriteRule    ^(.*)$ web/content/public/$1 [L]
   </IfModule>

但它不起作用。我该怎么办?

2 个答案:

答案 0 :(得分:0)

^(.*)$包含斜杠。所以不要在重写模式中包含斜杠。 但是在重写模式的头部包含一个根斜杠。

 RewriteRule    ^/clients(.*)$ /web/content/clients$1 [L]
 RewriteRule    ^(.*)$ /web/content/public$1 [L]

检查apache访问日志和错误日志,以查看返回的请求URL类型。

check the documentation,尤其是标有“以下是所有可能的替代组合及其含义的列表:”

答案 1 :(得分:0)

试试这个:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^/clients/$ web/content/clients/ [L]
RewriteRule ^/(.*)$ web/content/public/$1 [L]

如果要在.htaccess文件中使用该规则,请从模式中删除前导斜杠。