【发布时间】:2015-03-17 02:39:26
【问题描述】:
是的,我已经阅读了关于 SO 的其他类似问题,这就是我想出这个可能的解决方案的方式:
# Turn on the mod_rewrite engine
RewriteEngine On
# IF the request filename with .php extension is a file which exists
RewriteCond %{REQUEST_FILENAME}.php -f
# AND the request is not for a directory
RewriteCond %{REQUEST_URI} !/$
# Redirect to the php script with the requested filename
RewriteRule (.*) $1\.php [L]
这可以通过重写请求来实现,例如:/about-us 到 about-us.php,但是如果你转到 /about-us/,它不起作用。
我尝试了以下方法,但没有成功:
# Redirect to the php script with the requested filename
RewriteRule (.*)/? $1\.php [L]
和第二个RewriteCond有关系吗?
基本上,如果请求是针对真实文件或目录的,我希望它失败(如果可能),但如果两者都不存在,那么无论请求是否有结束斜杠,它都应该重写为 PHP 脚本。
这个可以吗?
【问题讨论】:
标签: regex apache .htaccess mod-rewrite url-rewriting