使用mod_rewrite从网址结尾处隐藏的.php [英] Using mod_rewrite to hide .php from the end of URLs
问题描述
我有一个网站,所有的页面都是PHP脚本,所以最终的URL的.php。
我添加以下到.htaccess文件,现在我可以访问.php文件没有.php扩展名:
RewriteEngine叙述在重写#开启的RewriteCond%{} REQUEST_FILENAME -f的.php#如果用的.php上月底所要求的文件存在
重写规则^(。*)$ $#1.PHP服务PHP文件
到目前为止好。但现在我想添加的所有.php文件重定向使我的控制范围之外的任何旧链接重定向到URL的新版本。
我试过这样:
RewriteEngine叙述在重写#开启的RewriteCond%{REQUEST_URI}。* \\。PHP
重写规则^(。*)\\。PHP $ http://example.com/$1 [R =永久的,L]的RewriteCond%{} REQUEST_FILENAME -f的.php#如果用的.php上月底所要求的文件存在
重写规则^(。*)$ $ 1.PHP [L]#服务的PHP文件
但似乎发送重定向即使不以.php结尾的URL,所以我得到停留在一个无限循环。任何其他组合我尝试似乎不符合要求(留下我在page.php)或所有请求(我拿死循环)。
RewriteEngine叙述在的RewriteCond%{} THE_REQUEST ^ \\ w + \\ /(。*)\\。PHP(\\ *?)?\\ HTTP /
重写规则^ HTTP://%{HTTP_HOST} /%1 [R = 301]的RewriteCond%{} REQUEST_FILENAME -f的.php
重写规则。* $ 0.php
只有%{THE_REQUEST}
中,在第二个规则发生(内部重定向无法重写%{REQUEST_URI}
,另一方面,是)。
I've got a site where all the pages are php scripts, so the URLs end .php.
I've added the following to a .htaccess file, and I can now access the .php files without the .php extension:
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
So far so good. But now I want to add a Redirect on all the .php files so that any old links outside of my control get redirected to the new version of the URL.
I've tried this:
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
but that seems to send a redirect even for URLs that don't end in .php, so I get stuck in an infinite loop. Any other combination I try seems to match no requests (and leave me at page.php) or all requests (and get me stuck in a loop).
RewriteEngine On
RewriteCond %{THE_REQUEST} ^\w+\ /(.*)\.php(\?.*)?\ HTTP/
RewriteRule ^ http://%{HTTP_HOST}/%1 [R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .* $0.php
Only %{THE_REQUEST}
is not rewritten in the internal redirection that happens in the second rule (%{REQUEST_URI}
, on the other hand, is).
这篇关于使用mod_rewrite从网址结尾处隐藏的.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!