如何重定向我所有的网址,没有扩展到结尾的.php有的、我所、扩展到、结尾

2023-09-02 00:38:35 作者:28.一罐栗子酱

我本来想拥有我所有的网址结束,没有延期。不幸的是,我已经尝试了许多的htaccess codeS和我差不多放弃了。

所以,现在我希望把它因此,如果一个人想要访问一个网页在我的网站,但忘记输入的.php,他/她将被自动重定向到相同的URL,但以.php

如何才能做到这一点?谢谢!

解决方案

下面是规则:

 的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{REQUEST_FILENAME}  PHP -f
重写规则^(。+)$ $ 1.PHP [L,QSA]
 

这将检查请求的资源不是现有的文件夹。例如:您申请 http://www.example.com/help 。如果有这样的文件夹present( /帮助)的规则将做什么(优先考虑的是一个文件夹)。 如果你不希望这种行为再取出第一行。

它会检查是否有这样的PHP文件重写之前。例如:您申请 http://www.example.com/aboutus 但没有 aboutus.php 文件有 - 不会发生重写

所有这些要求应该是没有结尾的斜线:应 http://www.example.com/aboutus 和NOT HTTP ://www.example.com/aboutus/

该规则将在子文件夹中的网址以及工作:例如: http://www.example.com/pages/help/aboutus 将被改写就好了。

由于上述检查规则将不会进入一个重写环(500号错误此规则)

查询字符串(页面参数)将是preserved

http response

I originally wanted to have all my urls end with no extension. Unfortunately, I've tried many htaccess codes and I've just about given up.

So now I want to make it so if a person wants to visit a page in my site, but forgets to enter .php, he/she will automatically be redirected to the same url but with the .php

How can this be done? Thanks!

解决方案

Here are the rules:

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

It will check if requested resource is not a existing folder. For example: you requesting http://www.example.com/help. If there is such folder present (/help) the rule will do nothing (priority is given to a folder). If you do not want this behaviour then remove the first line.

It will check if there is such .php file before rewriting. For example: you requesting http://www.example.com/aboutus but there is NO aboutus.php file there -- no rewrite will occur.

All such requests should be without trailing slash: should be http://www.example.com/aboutus and NOT http://www.example.com/aboutus/

The rule will work for URL in subfolders as well: e.g. http://www.example.com/pages/help/aboutus will be rewritten just fine.

Because of the above checks the rule will not enter into a rewrite loop (no 500 error on this rule)

Query string (page parameters) will be preserved