htaccess删除index.php,除非请求包含特定单词

时间:2018-06-27 07:19:17

标签: regex apache mod-rewrite

我有一个重写规则集,该规则集从URL中剥离了index.php并将其重新映射,并且运行良好。但是,如果字符串包含某个要使用的单词,我将无法忽略该规则。

因此,基本上,我需要在URL为http://domain.url/foo/bar时重写,而在http://domain.url/shaz/foo/bar时不需要重写

# Remove 'index.php' from url
# 1) redirect the client from "/index.php/foo/bar" to "/foo/bar"
RewriteCond %{THE_REQUEST} /index\.php/(.+)\sHTTP [NC]
# ignore rule if
RewriteCond %{REQUEST_URI} !^shaz [NC]
RewriteCond %{THE_REQUEST} !^shaz  [NC]
# end ignore
RewriteRule ^ /%1 [NE,L,R]

# 2) internally map "/foo/bar" to "/index.php/foo/bar"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php/$1 [L]
# END 

我该怎么做才能使其正常工作?

1 个答案:

答案 0 :(得分:0)

您可以在网站根.htaccess中使用以下规则:

# Remove 'index.php' from url
# 1) redirect the client from "/index.php/foo/bar" to "/foo/bar"
RewriteCond %{THE_REQUEST} /index\.php/(?!shaz)(\S+)\sHTTP [NC]
RewriteRule ^ /%1 [NE,L,R]

# 2) internally map "/foo/bar" to "/index.php/foo/bar"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php/$1 [L]
# END 

(?!shaz)为否定超前行为,如果shaz之后是/index.php/

,则跳过匹配