.htaccess为规则组添加一个!not条件

时间:2012-10-03 07:03:18

标签: .htaccess mod-rewrite clean-urls

为了容纳一些ajax我正在进行基本的.htaccess重写。

文件夹结构

main.html
alpha/
      alpha.php
      alpha.html

main.html包含一个调用alpha / alpha.php的div alpha / alpha.html也包含alpha.php的静态包含。

我想要一个干净的网址www.myUrl.com/alpha,它会调用alpha / alpha.html。

如何为一组规则添加!NOT条件,因此我不必将其包含在每个规则中,例如从组中的每个规则中删除[^(html|php)],即

set the following condition
^[^(html|php)]$
for

RewriteRule ^(.*)_aaa$ %{DOCUMENT_ROOT}/$1/$1.html [L]
RewriteRule ^(.*)_bbb$ %{DOCUMENT_ROOT}/$1/$1.html [L]
RewriteRule ^(.*)_ccc$ %{DOCUMENT_ROOT}/$1/$1.html [L]

end condition and do not apply for 
RewriteRule ^(.*)_ddd$ %{DOCUMENT_ROOT}/$1/$1.html [L] 
RewriteRule ^(.*)_eee$ %{DOCUMENT_ROOT}/$1/$1.html [L]`

当前.htaccess

Options +ExecCGIn
Options -indexes

DirectoryIndex main.html

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

# main start page
RewriteRule ^main$ main.html [L] 

# clean for alpha
# myUrl.com/alpha
RewriteRule ^(.*)[^(html|php)]$ %{DOCUMENT_ROOT}/$1/$1.html [L] 

thx Art

1 个答案:

答案 0 :(得分:0)

尝试使用负面后卫:

RewriteRule ^(.*)(?<!html|php)$ %{DOCUMENT_ROOT}/$1/$1.html [L] 

时髦的小(?<!html|php)$表示:字符串的结尾($,前面有htmlphp


编辑:

鉴于:

set the following condition
^[^(html|php)]$
for

RewriteRule ^(.*)_aaa$ %{DOCUMENT_ROOT}/$1/$1.html [L]
RewriteRule ^(.*)_bbb$ %{DOCUMENT_ROOT}/$1/$1.html [L]
RewriteRule ^(.*)_ccc$ %{DOCUMENT_ROOT}/$1/$1.html [L]

end condition and do not apply for 
RewriteRule ^(.*)_ddd$ %{DOCUMENT_ROOT}/$1/$1.html [L] 
RewriteRule ^(.*)_eee$ %{DOCUMENT_ROOT}/$1/$1.html [L]`

你想:

# If this matches, (the opposite of !(html|php)), skip all of the following rules (3 of them)
RewriteRule \.(php|html)$ - [S=3]

RewriteRule ^(.*)_aaa$ %{DOCUMENT_ROOT}/$1/$1.html [L]
RewriteRule ^(.*)_bbb$ %{DOCUMENT_ROOT}/$1/$1.html [L]
RewriteRule ^(.*)_ccc$ %{DOCUMENT_ROOT}/$1/$1.html [L]

# if first condition matched, we skipped to here
RewriteRule ^(.*)_ddd$ %{DOCUMENT_ROOT}/$1/$1.html [L] 
RewriteRule ^(.*)_eee$ %{DOCUMENT_ROOT}/$1/$1.html [L]`