我正在重写我的网址以摆脱.php
扩展名(并希望获得变量,因为我得到的所有帮助都很棒,但仍然无法使用。
这是我的代码:
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,但随后出现错误:
Not Found
The requested URL /index.php was not found on this server.
同时,如果我取出该代码,它可以正常工作,但具有.php
扩展名
答案 0 :(得分:3)
我在 .htaccess 中使用此代码删除 .php 扩展程序,并将 .php 扩展程序页重定向到非 .php < / strong>扩展页面:
<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 中使用上述代码时,您可以访问不带扩展程序的 PHP 页面,以及当用户访问 .php的页面扩展名,他/她将被重定向到没有扩展名的页面。
示例:
在内部使用扩展名访问PHP文件时不会有任何问题:
<?
include("page.php");
?>
将毫无问题地工作。
答案 1 :(得分:1)
将此代码放在/myproject/.htaccess
(不在root中):
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /myproject/
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} /([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/myproject/$1.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
然后从新的浏览器中测试它。