![](/img/trans.png)
[英]RedirectMatch 301 rule conflicts with RewriteRule in htaccess
[英]Prevent rules conflicts with htaccess' RewriteRule
我想使用一些RewriteRule
来转换:
http://example.com/blah => index.php?id=blah
http://example.com/someone/blah => index.php?id=blah&user=someone
这是htaccess
:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?id=$1 [L]
RewriteRule ^(.*)/(.*)$ /index.php?id=$2&user=$1 [L]
</IfModule>
但是似乎最后两个规则是不兼容的:使用此htaccess
,我收到一些500 Internal Server Error
。
如何创建两个规则,以使它们彼此不“重叠”?
注意事项:
这两个规则中的每一个单独起作用
当我使用第二条规则时,则http://example.com/someone/blah => index.php?id=blah&user=someone
可以工作,但是似乎根文件夹不再是/
but /someone/
,然后找不到CSS ...在这种情况下如何防止更改基本文件夹?
您有4个问题:
(.*)
会将任何内容转换为/index.php?id=$1
500 internal server error
。 domain.com/anything/anything
失败 要解决重定向问题,可以使用以下方法:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /index.php?id=$2&user=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?id=$1 [L]
</IfModule>
由于我已经更改了可捕获所需数据的正则表达式(改为([^/]+)
,表示不是/
任何东西),因此在这种情况下顺序无关紧要,因为它会特别匹配:
domain.com/anything
和
domain.com/anything/anything
要修复CSS和图片,您可以使用base
TAG在HTML中定义绝对URL:
<base href="http://domain.com/">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.