htaccess在url中隐藏文件扩展名

时间:2013-10-29 12:25:07

标签: apache .htaccess mod-rewrite

我想在浏览器中隐藏我的文件扩展名以显示,例如http://www.example.com/dir/abc.php应仅显示为http://www.example.com/dir/abc

我在.htaccess

上写了关于rewriteCond和重写规则的内容
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]

但这段代码似乎无法帮助我。

编辑: 这是我的htaccess文件的完整代码

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]

5 个答案:

答案 0 :(得分:1)

完成.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]

答案 1 :(得分:1)

最简单最简单的方法:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

答案 2 :(得分:0)

 RewriteEngine On

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.php

答案 3 :(得分:0)

简单的方法是:

you create your  link like

'<a href="home">Home</a>'而不是'<a href="home.php">Home</a>'
然后你的.htaccess应该是这样的:

RewriteCond %(REQUEST_FILENAME) !-d
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-l
RewriteCond ^home home.php [L]

答案 4 :(得分:0)

我在任何方面都不是Apache专家,但我很好奇为什么没有人推荐谈判模块(mod_negotiation)。是否存在避免使用

的最佳实践原因

我在http://www.webhostingtalk.com/showthread.php?t=317269传递了一下。在我的全局配置中启用mod_negotiation后:

LoadModule negotiation_module /usr/lib/apache2/modules/mod_negotiation.so

我只需要在我的虚拟主机选项中启用它。这是整个配置块:

DocumentRoot /var/www/site_directory/public_html
ServerName your.domain.here.com
<Directory /var/www/site_directory/public_html>
allow from all
Options +Indexes Multiviews
</Directory>
相关问题