简体   繁体   English

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

[英].htaccess allow extension, allow no extension, and allow trailing slash

I am having trouble coding my htaccess file to do what I need it to do.我在编写 htaccess 文件以执行我需要它执行的操作时遇到问题。

Here is what I have so far:这是我到目前为止所拥有的:

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]

So from my testing and my knowledge what this does is:因此,根据我的测试和我的知识,它的作用是:

  • sets up a 404 and 500 error page redirect设置 404 和 500 错误页面重定向
  • any pages that end with a slash, redirect to .php任何以斜杠结尾的页面,重定向到 .php
  • any pages that end with a any extension, redirect to .php任何以任何扩展名结尾的页面,重定向到 .php

This does what im wanting HOWEVER I don't want to have to add each page like: RewriteRule ^index/?$ index.php [NC] every time I create a new page so how do I write a RewriteRule that does the same thing (allows the page with a slash and without) but for all domain pages?这就是我想要的但是我不想添加每个页面,例如: RewriteRule ^index/?$ index.php [NC]每次我创建一个新页面时,我该如何编写一个RewriteRule来做同样的事情(允许页面带斜线和不带)但对于所有域页面?

You can use:您可以使用:

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

Instead of all lines from RewriteRule ^index/?$ index.php ...而不是RewriteRule ^index/?$ index.php的所有行...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM