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

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

问题描述

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

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

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

解决方案

下面是规则:

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

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

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

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

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

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

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

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]

  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.

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

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

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

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

  6. Query string (page parameters) will be preserved

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

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