删除URL .php扩展名会出现404错误 [英] Removing URL .php extension gives 404 error
问题描述
我正在重写自己的网址,以摆脱 .php
扩展名(希望get变量也可以,因为我得到的所有帮助都很棒,但仍然无法正常工作。
I am rewriting my url to get rid of the .php
extension (and hopefully the get variables too as all the help I have gotten is great, but still wont work.
这是我的代码:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+?)/?$ $1.php [NC,L]
现在当我使用此代码时,它将.php删除,但是错误提示:
Now when I use this code, it takes the .php out, but then the error says:
Not Found
The requested URL /index.php was not found on this server.
同时,如果我删除该代码,它可以正常工作,但是具有 .php
扩展名
Meanwhile, if I take that code out, it works fine, but has the .php
extension
推荐答案
我在 .htaccess 中使用此代码删除。 php 扩展名,并将 .php 扩展名页面重定向到非 .php 扩展名页面:
I use this code in .htaccess to remove .php extension and redirect .php extension pages to non .php extension pages :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(wp-admin)($|/) - [L] # You don't want to mess with WordPress
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule .* $0.php
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
</IfModule>
在 .htaccess 中使用上述代码时,您可以访问<没有扩展名的strong> PHP 页面,当用户访问具有扩展名 .php 的页面时,他/她将被重定向到没有扩展名的页面。
When the above code is used in .htaccess, you can access PHP pages without the extension and when a user visits a page with .php extension, he/she will be redirected to the page without the extension.
示例:
http://example.com/page.php 重定向到 http:// example。 com / page
http://example.com / page 与
http://example.com/page.php
内部访问扩展名为PHP的文件不会有任何问题:
There wouldn't be any problem when you access PHP files with extension internally :
<?
include("page.php");
?>
可以正常工作。
这篇关于删除URL .php扩展名会出现404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!