我希望对我的网站的请求按如下方式工作:
http://example.com/理想情况下会拉出index.php文件(当前默认行为)而不显示index.php
http://example.com/foo.php将按预期拉出 foo.php
http://example.com/blerg将重定向到 http://example.com/bar.php?code=blerg
我现在有以下重写规则
RewriteRule ^/(.*\.(php|html|htm|css|jpg))$ /$1 [NC,L]
RewriteRule ^/(.*)$ /bar.php?code=$1 [NC,L]
几乎可以工作,除了 http://example.com/拉出 bar.php 而不是 index.php
理想情况下,我不必在第一条规则中包含所有可能的文件扩展名,我宁愿它只检测它是否是实际文件。
最佳答案
我意识到这并不完全是您所要求的,但我经常在 .htaccess 中使用它:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
..将任何不是实际文件或目录的内容发送到index.php,然后index.php 包含解释URL 字符串中内容的逻辑。
例如
$url_array = split('/', $_SERVER['REQUEST_URI']);
array_shift($url_array); // remove first value as it's empty
关于regex - Apache mod 重写简单重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/838080/