我的重写规则是:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^([A-Za-z0-9_]*)/?$ index.php?a=$1 [NC]
RewriteRule ^([A-Za-z0-9_]*)/([A-Za-z0-9_]*)/?$ index.php?a=$1&b=$2 [NC]
RewriteRule ^([A-Za-z0-9_]*)/([A-Za-z0-9_]*)/([A-Za-z0-9_]*)/?$ index.php?a=$1&b=$2&c=$3 [NC]
URL http://example.com/home/test/b将返回内部等效的findex.php?a=home&b=test&c=b。虽然这是伟大的(我张贴在这里昨天试图让mod_rewrite工作),我想做一个url像http://example.com/home/test/b.php内部调用index.php?a=home&b=test&c=b.p p,而不是让Apache试图找到(不成功)/var/www/home/test/b.p p我想让我的重写规则实际上处理所有的文件扩展名
可以使用以下代码:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(['w.]+)/?$ index.php?a=$1 [L,QSA]
RewriteRule ^('w+)/(['w.]+)/?$ index.php?a=$1&b=$2 [L,QSA]
RewriteRule ^('w+)/('w+)/(['w.]+)/?$ index.php?a=$1&b=$2&c=$3 [L,QSA]
字符类中的 ['w.]
将允许[a-zA-Z0-9._]
如果您只想处理文件扩展名,您可以手动完成,通过将最后一行更改为:
RewriteRule ^([A-Za-z0-9_]*)/([A-Za-z0-9_]*)/([A-Za-z0-9_]*('.[a-z]+)?)/?$ index.php?a=$1&b=$2&c=$3 [NC]
通过添加('.[a-z]+)?
,这将处理http://example.com/home/test/b.php
,然后在index.php
中添加var_dump($_GET);
:
array(3) { ["a"]=> string(4) "home" ["b"]=> string(4) "test" ["c"]=> string(5) "b.php" }
希望有帮助。
注意:它只处理一个.
,在它之后至少有一个[a-z]
。
如果你想处理像http://example.com/home/test/b.test.php
这样的东西,你需要:
RewriteRule ^([A-Za-z0-9_]*)/([A-Za-z0-9_]*)/([A-Za-z0-9_]*('.[a-z]+)*)/?$ index.php?a=$1&b=$2&c=$3 [NC]