如何从URL中删除html和php扩展

时间:2016-10-18 15:39:17

标签: php html .htaccess

您好我试图隐藏我的网站网址中的html和php扩展程序,而这里是我的.htaccess文件

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/install -d
RewriteCond %{REQUEST_URI} !(install) [NC]
RewriteRule ^(.*) /install/ [L,redirect=302]
RewriteCond %{REQUEST_URI} ^(.+)\!$
RewriteRule ^(.*) stats.php?url=$1 [L]
RewriteCond %{REQUEST_URI} ^(.+)\~s$
RewriteRule ^(.*) stats.php?url=$1 [L]
RewriteCond %{REQUEST_URI} ^(.+)\~q$
RewriteRule ^(.*) generate_qr.php?url=$1 [L]
RewriteCond %{REQUEST_URI} ^(.+)\~p$
RewriteRule ^(.*) preview_url.php?url=$1 [L]
RewriteCond %{REQUEST_URI} !^(.+)\.html$
RewriteCond %{REQUEST_URI} !^(.+)\.htm$
RewriteCond %{REQUEST_URI} !^(.+)\.php$
RewriteCond %{REQUEST_URI} !^(.+)\.js$
RewriteCond %{REQUEST_URI} !^(.+)\.css$
RewriteCond %{REQUEST_URI} !^(.+)\.jpg$
RewriteCond %{REQUEST_URI} !^(.+)\.png$
RewriteCond %{REQUEST_URI} !^(.+)\.gif$
RewriteCond %{REQUEST_URI} !^(.+)\.swf$
RewriteCond %{REQUEST_URI} !^(.+)\.xml$
RewriteCond %{REQUEST_URI} !^(.+)\.ico$
RewriteCond %{REQUEST_URI} !^(.+)\.txt$
RewriteCond %{REQUEST_URI} !^index\.html$
RewriteCond %{REQUEST_URI} !^index\.php$
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_URI} !^(.+)/$
RewriteRule ^(.*) url_redirector.php?url=$1 [L]
RewriteRule ^(.*).html$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.html [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

任何想法我该怎么做?我已经尝试了很多我找到的答案,但这对我没有用。

2 个答案:

答案 0 :(得分:3)

您可以重构规则以减少大量冗余,并有2个额外的规则来隐藏.php.html扩展名:

RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}/install -d
RewriteRule !^install/ /install/ [L,NC,R=302]

RewriteRule ^(.+(?:!|\~s))$ stats.php?url=$1 [L,QSA]

RewriteRule ^(.+\~[pq])$ preview_url.php?url=$1 [L,QSA]    

RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteCond %{REQUEST_URI} !\.(php|js|css|jpe?g|png|gif|swf|ico|xml|txt|html?)$ [NC]
RewriteCond %{REQUEST_URI} !^/index\.(php|html)$ [NC]
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* url_redirector.php?url=$0 [L,QSA]

答案 1 :(得分:2)

删除扩展程序

要从PHP文件中删除.php扩展名,您必须在.htaccess文件中添加以下代码:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

如果要从html文件中删除.html扩展名

RewriteRule ^([^\.]+)$ $1.html [NC,L]

删除.html

  RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^(.*)$ $1.html

更新

  RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^.]+)$ $1.html [NC,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]