删除友好的URL的.php扩展名(明确写成)

htaccess删除我网站文件的.php扩展名。


RewriteEngine on

RewriteBase /

RewriteCond %{SCRIPT_FILENAME} !-d

RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^(.*)$ $1.php [L,QSA]

现在,如果我去我的网站


www.mysite.com/home

工作正常,它重定向到home.php,但URL仍然友好。


但是如果我写这个URL:


www.mysite.com/home.php

已提供home.php,且URL不友好。


如何避免这种行为?我希望用户写www.mysite.com/home.php,URL栏中显示的URL是www.mysite.com/home


慕侠2389804
浏览 559回答 3
3回答

慕田峪4524236

您可以像这样从请求中删除.phpRewriteCond %{REQUEST_URI} ^/(.*).php$RewriteRule ^(.*)$ %1 [L,QSA]

慕码人8056858

码RewriteEngine OnRewriteBase /# remove enter code here.php; use THE_REQUEST to prevent infinite loopsRewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTPRewriteRule (.*)\.php$ $1 [R=301]# remove indexRewriteRule (.*)/index$ $1/ [R=301]# remove slash if not directoryRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_URI} /$RewriteRule (.*)/ $1 [R=301]# add .php to access file, but don't redirectRewriteCond %{REQUEST_FILENAME}.php -fRewriteCond %{REQUEST_URI} !/$RewriteRule (.*) $1\.php [L]

凤凰求蛊

您还可以将以下代码放入.htaccess文件中:options +multiviews这是在当前目录中搜索可用的扩展名(我认为是.html,.htm和.php)。因此,例如,如果您请求/user它将查找user.php。
打开App,查看更多内容
随时随地看视频慕课网APP