【问题标题】:Rewrite .htaccess to prevent user visit "index"重写 .htaccess 以防止用户访问“索引”
【发布时间】:2017-11-23 19:33:23
【问题描述】:

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

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>

【问题讨论】:

    标签: html apache .htaccess mod-rewrite


    【解决方案1】:

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

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

    它只在http://example.com/index 工作,只在根目录工作,对于另一个子文件夹,它不起作用。谁有更好的答案?

    更新:

    <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>
    

    这段代码是一种愚蠢的处理方式,但如果我有数千个文件夹,它会非常复杂。

    【讨论】:

      【解决方案2】:

      最后,这个方法可以将所有“索引”重定向到错误404的错误页面。

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

      但是http://example.com/ 加载时出现问题。 js 和 css 文件似乎取消链接。使用回笨方法可以访问js和css文件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-23
        • 1970-01-01
        • 1970-01-01
        • 2013-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多