gpt4 book ai didi

php - 使用 mod_rewrite 隐藏 URL 末尾的 .php

转载 作者:可可西里 更新时间:2023-11-01 13:20:20 25 4
gpt4 key购买 nike

我有一个网站,所有页面都是 php 脚本,所以 URL 以 .php 结尾。

我已将以下内容添加到 .htaccess 文件中,现在我可以访问没有 .php 扩展名的 .php 文件:

RewriteEngine On  # Turn on rewriting

RewriteCond %{REQUEST_FILENAME}.php -f # If the requested file with .php on the end exists
RewriteRule ^(.*)$ $1.php # serve the PHP file

到目前为止一切顺利。但现在我想在所有 .php 文件上添加一个重定向,以便我控制之外的任何旧链接都被重定向到新版本的 URL。

我已经试过了:

RewriteEngine On  # Turn on rewriting

RewriteCond %{REQUEST_URI} .*\.php
RewriteRule ^(.*)\.php$ http://example.com/$1 [R=permanent,L]

RewriteCond %{REQUEST_FILENAME}.php -f # If the requested file with .php on the end exists
RewriteRule ^(.*)$ $1.php [L] # serve the PHP file

但是即使对于不以 .php 结尾的 URL,这似乎也会发送重定向,所以我陷入了无限循环。我尝试的任何其他组合似乎都不匹配任何请求(让我留在 page.php)或所有请求(让我陷入循环)。

最佳答案

RewriteEngine On

RewriteCond %{THE_REQUEST} ^\w+\ /(.*)\.php(\?.*)?\ HTTP/
RewriteRule ^ http://%{HTTP_HOST}/%1 [R=301]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .* $0.php

只有 %{THE_REQUEST} 没有在第二条规则中发生的内部重定向中被重写(另一方面,%{REQUEST_URI} 是)。

关于php - 使用 mod_rewrite 隐藏 URL 末尾的 .php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3086325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com