如何使用 Apache Mod_rewrite 删除 php 扩展,同时保留 GET 参数? [英] How to use Apache Mod_rewrite to remove php extension, while preserving the GET parameters?

查看:17
本文介绍了如何使用 Apache Mod_rewrite 删除 php 扩展,同时保留 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} (.phpsHTTP/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} (.phpsHTTP/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 扩展被移除,参数被保留,但页面不会被提供,因为浏览器说有太多的重定向.

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} (.phpsHTTP/1)
RewriteRule ^(.+).php$ /$1 [R=301,L,QSA]

当用户请求一个 PHP 文件时执行外部重定向.这里的 RewriteCond 是为了防止重定向循环——即由于内部重写和外部重定向不当而导致的无限重定向(删除 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.1 Something like this, so the pattern in the RewriteCond won't work since it didn't take parameters into account.

要解决它,只需将上面的代码替换为:

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.

希望这能帮助有类似问题的人(或者它只是像我这样的菜鸟?lol)

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

这篇关于如何使用 Apache Mod_rewrite 删除 php 扩展,同时保留 GET 参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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