[英]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.