使用mod_rewrite从URL末尾隐藏.php

rjm*_*nro 6 php apache mod-rewrite seo

我有一个网站,其中所有页面都是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
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.但现在我想在所有.php文件上添加一个Redirect,以便我控制之外的任何旧链接都被重定向到新版本的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
Run Code Online (Sandbox Code Playgroud)

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

Art*_*cto 7

RewriteEngine On

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

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .* $0.php
Run Code Online (Sandbox Code Playgroud)

只是%{THE_REQUEST}不在第二个规则中发生的内部重定向中重写(%{REQUEST_URI}另一方面,是).