2017-06-21 49 views
1

这里是我的.htaccess,我用404自定义页面并隐藏.html和.php扩展名。但是,当我阻止用户输入.html和.php时,他们也可以访问http://example.com/index和子文件夹http://example.com/folder/index。我使用相对url链接图像和声明,用户输入“索引”将使其不连接。如何使用.htaccess将任何“索引”重定向到/error404.html?重写.htaccess以防止用户访问“索引”

ErrorDocument 404 /error404.html 

<IfModule mod_rewrite.c> 
    RewriteCond %{THE_REQUEST} \.(?:html|php)\s [NC] 
    RewriteCond %{REQUEST_URI} !=/error404.html 
    RewriteRule^- [R=404,L] 
    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> 

回答

0

我只是解决了一半.... 的代码是在这里:

<Files "index"> 
RewriteCond %{REQUEST_URI} !=/error404.html 
RewriteRule^- [R=404,L] 
</Files> 

它只是在http://example.com/index工作,只有在根目录下工作,另一个子文件夹,它不能正常工作。任何人有更好的答案?

UPDATE:

<Files "index"> 
RewriteCond %{REQUEST_URI} !=/error404.html 
RewriteRule^- [R=404,L] 
</Files> 
<Files "folder/index"> 
RewriteCond %{REQUEST_URI} !=/error404.html 
RewriteRule^- [R=404,L] 
</Files> 

此代码是上悬而未决的一个笨方法,但如果我有文件夹的千年,它会非常复杂。

0

最后,这种方法可以在http://example.com/负载重定向所有的“指数”的错误页面,错误404

RewriteEngine on 
RewriteCond %{REQUEST_URI} index 
RewriteCond %{REQUEST_URI} !=/error404.html 
RewriteRule^- [R=404,L] 

但蹊跷的。 js和css文件看起来像是取消链接。使用后面的笨方法可以访问js和css文件。