1

我想在浏览器中隐藏要显示的文件扩展名,就像http://www.example.com/dir/abc.php应该只显示一样http://www.example.com/dir/abc

我写了以下 rewriteCond 和 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]

但这段代码似乎对我没有帮助。

编辑:这是我的 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]
4

5 回答 5

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]
于 2013-10-29T14:20:32.783 回答
0

简单的方法是:

you create your  link like

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

RewriteCond %(REQUEST_FILENAME) !-d
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-l
RewriteCond ^home home.php [L]
于 2013-12-30T13:24:09.443 回答
0

最简单最简单的方法:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
于 2015-03-28T13:40:50.873 回答
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>
于 2014-04-02T02:38:13.567 回答
0
 RewriteEngine On

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.php
于 2013-10-29T12:40:14.143 回答