通过 htaccess 在 url 中隐藏文件扩展名 [英] hide file extension in url by htaccess
本文介绍了通过 htaccess 在 url 中隐藏文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在浏览器中隐藏要显示的文件扩展名,例如 http://www.example.com/dir/abc.php
应显示为 http://仅限 www.example.com/dir/abc
.
I want to hide my file extensions in browser to be displayed, like http://www.example.com/dir/abc.php
should be displayed as http://www.example.com/dir/abc
only.
我在 .htaccess
上写了以下 rewriteCond 和重写规则
i wrote following rewriteCond and rewrite rule on .htaccess
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]
但是这段代码似乎对我没有帮助.
but this code doeesn't seems to helping me.
这是我的 htaccess 文件的完整代码
this is complete code of my htaccess file
Options -Indexes
ErrorDocument 403 /errors/403.php
ErrorDocument 404 /errors/404.php
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(www\.)?127.0.0.1 [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?127.0.0.1.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost.*$ [NC]
RewriteRule \.(js|css|jpg|png)$ - [F]
RewriteEngine on
RewriteRule ^(.*)\.[\d]{10}\.(css|js|png|jpg)$ $1.$2 [L]
RewriteEngine On
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
推荐答案
COMPLETE .htaccess:
Options +FollowSymLinks -MultiViews
ErrorDocument 403 /errors/403.php
ErrorDocument 404 /errors/404.php
RewriteEngine on
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
# block direct hot linking
RewriteCond %{HTTP_REFERER} !^http://(www\.)?127.0.0.1 [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost [NC]
RewriteRule \.(js|css|jpg|png)$ - [F]
RewriteRule ^(.*)\.[\d]{10}\.(css|js|png|jpg)$ $1.$2 [L]
这篇关于通过 htaccess 在 url 中隐藏文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文