.htaccess允许扩展,不允许扩展,并允许尾部斜杠

时间:2017-10-21 12:06:17

标签: php html .htaccess redirect

我无法对htaccess文件进行编码以完成我需要它做的事情。

这是我到目前为止所做的:

RewriteOptions inherit
ErrorDocument 404 http://www.tempsite.com/400.php
ErrorDocument 500 http://www.tempsite.com/500.php

RewriteEngine On

RewriteRule ^index/?$ index.php  [NC]
RewriteRule ^pricing/?$ pricing.php  [NC]
RewriteRule ^support/?$ support.php  [NC]
RewriteRule ^about/?$ about.php  [NC]
RewriteRule ^learning/?$ learning.php  [NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]

因此,根据我的测试和我的知识,它的作用是:

  • 设置404和500错误页面重定向
  • 任何以斜杠结尾的网页,重定向到.php
  • 任何以任何扩展名结尾的网页,重定向到.php

这就是我想要的但是我不想每次创建新页面时都要添加每个页面:RewriteRule ^index/?$ index.php [NC]所以如何编写一个RewriteRule来做同样的事情(允许带有斜杠的页面,但是对于所有域页面都允许?

1 个答案:

答案 0 :(得分:2)

您可以使用:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

而不是来自RewriteRule ^index/?$ index.php ...

的所有行
相关问题