如何重定向我所有没有扩展名的 url 以 .php 结尾 [英] How to redirect all my urls with no extension to end with .php

查看:36
本文介绍了如何重定向我所有没有扩展名的 url 以 .php 结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初想让我的所有网址都以没有扩展名的方式结束.不幸的是,我尝试了很多 htaccess 代码,但我几乎放弃了.

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.

所以现在我想这样做,如果有人想访问我网站中的页面,但忘记输入 .php,他/她将自动重定向到相同的 url,但使用 .php

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!

推荐答案

规则如下:

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

  1. 它将检查请求的资源是否不是现有文件夹.例如:您请求 http://www.example.com/help.如果存在这样的文件夹 (/help),则规则将不执行任何操作(优先考虑文件夹).如果您不想要这种行为,请删除第一行.

  1. 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.

它会在重写之前检查是否有这样的.php文件.例如:您请求 http://www.example.com/aboutus 但那里没有 aboutus.php 文件——不会发生重写.

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.

所有此类请求都应该没有尾部斜杠:应该是 http://www.example.com/aboutus 而不是 http://www.example.com/关于我们/

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

该规则也适用于子文件夹中的 URL:例如http://www.example.com/pages/help/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.

由于上述检查,规则不会进入重写循环(此规则没有 500 错误)

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

这篇关于如何重定向我所有没有扩展名的 url 以 .php 结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆