如何使用.htaccess隐藏扩展名并阻止用户通过扩展程序访问网址?

时间:2017-06-20 14:39:01

标签: apache .htaccess mod-rewrite

这是我的代码,当用户查看我的网站http://example.com/xxx.htmlhttp://example.com/xxx时隐藏.html和.php扩展名 但是我希望在用户输入http://example.com/xxx.html时让他们无法访问并跳转到404找不到。

<IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine On
  RewriteBase /

# Add trailing slash to url
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
  RewriteRule ^(.*)$ $1/ [R=301,L]

# Remove .php-extension from url
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.php -f
  RewriteRule ^([^\.]+)/$ $1.php 

# Remove .html-extension from url
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.html -f
  RewriteRule ^([^\.]+)/$ $1.html 

# End of Apache Rewrite Rules
 </IfModule>

有人有解决方案吗?

1 个答案:

答案 0 :(得分:0)

您可以将其添加为第一条规则:

RewriteCond %{THE_REQUEST} \.(?:html|php)\s [NC]
RewriteRule ^ - [R=404,L]

<强>更新

这可能会解决您的ErrorDocument问题:

RewriteCond %{THE_REQUEST} \.(?:html|php)\s [NC]
RewriteCond %{REQUEST_URI} !=/error404.html
RewriteRule ^ - [R=404,L]