apache mod_rewrite规则编写

时间:2012-11-17 10:58:57

标签: php apache

在我的申请中,我正在尝试将http://domain.com/auth/login/auth/register关联到http://domain.com/?p=auth&action=$,其中$就是这样:http://domain.com/auth/$

我的代码是

RewriteEngine On
RewriteRule ^auth/(.+)$ ?p=auth&action=$1

但现在我的所有CSS和图像都搞砸了._。

1 个答案:

答案 0 :(得分:3)

看起来很好,除了我建议使用完整的网址代替重定向。为了减少与其他规则的交互,您应该添加[L]标志,以停止对该规则的匹配请求的重写过程:

RewriteEngine On
RewriteRule ^auth/(.+)$ http://domain.com/?p=auth&action=$1 [L]

一般提示:如果您有权访问服务器配置,那么将重写规则放在那里,而不是使用.htaccess样式重写。它更可靠,更容易配置:

RewriteEngine On
RewriteRule ^/auth/(.+)$ /?p=auth&action=$1 [L]

甚至性能稍好一点:

RewriteEngine On
RewriteRule ^/auth/(.+)$ /index.php?p=auth&action=$1 [L]

(我假设您使用文件index.php作为路由器,也可能是其他任何语言或解决方案。)