本文介绍了如何使用Apache的mod_rewrite删除PHP扩展,而preserving的GET参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从我的网站上删除了PHP扩展。当用户请求一个PHP文件时,PHP将被删除,用户会被重定向,而当没有PHP的URL的用户类型,实际的PHP文件将被提供。这种运作良好,除了当出现在URL GET参数。我的规则如下:

I was trying to remove the PHP extensions from my website. When user requests a PHP file, the PHP will be removed and the user will be redirected, and when the user types in an URL without PHP, the actual PHP file will be served. This worked well except when there is GET parameter in the URL. My rules are as below:

# remove .php ONLY if requested directly
RewriteCond %{THE_REQUEST} (\.php\sHTTP/1)
RewriteRule ^(.+)\.php$ /$1 [R=301,L,QSA]

# remove trailing slash ONLY if it is not an existing folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# rewrite to FILENAME.php if such file does exist and is not a folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L,QSA]

我想这应该已经能够即使有任何GET参数删除PHP,但失败了。我也尝试过这样的事情:

I thought this should already be able to remove php even when there is any GET parameter, but it failed. I also tried something like this:

RewriteCond %{THE_REQUEST} (\.php\sHTTP/1)
RewriteRule ^(.)\.php(.)$ $1$2 [R=301,L,QSA]

它也没有工作,PHP仍然存在。但是,如果我尝试:

It also didn't work, the php is still there. But if I try:

RewriteRule ^(.)\.php(.)$ $1$2 [R=301,L,QSA]

即去除的RewriteCond,PHP扩展被删除,参数为preserved,但随着浏览器说,有太多重定向的页​​面将不会投放。

ie, removing the RewriteCond, the php extension gets removed and the parameters were preserved, but the page won't be served as the browser says there were too many redirects.

任何人任何想法吗?

推荐答案

感谢您STEAP的回答。我碰巧能想出一个办法刚才来对付它,想在这里分享的情况下,其他人碰到类似的问题。

Thank you SteAp for your answer. I happened to be able to figure out a way to deal with it just now, and would like to share here in case others run into similar problems.

在我的规矩,我有

# remove .php ONLY if requested directly
RewriteCond %{THE_REQUEST} (\.php\sHTTP/1)
RewriteRule ^(.+)\.php$ /$1 [R=301,L,QSA]

要当用户请求的PHP文件执行外部重定向。这里的RewriteCond是prevent重定向循环 - 即因不当重写内部和外部重定向无尽的重定向(删除PHP,再加入PHP,然后再删除,...)

To execute external redirect when user requests a PHP file. The RewriteCond here is to prevent redirect loops - ie, endless redirect due to improper internal rewrite and external redirect (removing php, then adding php, then remove again, ...)

当有参数,实际的头当属 http://domain.com/file.php?.... HTTP / 1.1
像这样的东西,所以在对的RewriteCond模式是行不通的,因为它没有考虑参数考虑进去。

When there are parameters, the actual header comes as http://domain.com/file.php?.... HTTP/1.1Something like this, so the pattern in the RewriteCond won't work since it didn't take parameters into account.

要解决这个问题,只需更换上述code有:

To solve it, simply replace the above code with:

# remove .php ONLY if requested directly
RewriteCond %{THE_REQUEST} (\.php(.*)\sHTTP/1)
RewriteRule ^(.+)\.php$ /$1 [R=301,L,QSA]

这样的参数可以通过模式匹配,现在一切正常。

By doing so, the parameters can be matched by the pattern, and now everything works.

希望这会帮助别人有类似的问题(或者是它只是一个菜鸟喜欢我吗?笑)

Hope this will help someone having similar problem (or is it just a noob like me? lol)

这篇关于如何使用Apache的mod_rewrite删除PHP扩展,而preserving的GET参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 13:27