【问题标题】:Error document and rewriting URL错误文档和重写 URL
【发布时间】:2012-05-31 05:27:55
【问题描述】:

我想为一些 HTTP 错误使用自定义页面,所以我使用 .htaccess 文件。同时我也想用URL重写,但是不行,不知道为什么。

这是我的 .htaccess :

#Rewrite options
RewriteEngine On    # Turn on the rewriting engine

#Error
RewriteRule    ^error/([0-9]{3})/?$    index.php?page=error-$1 [NC,L]

#Other options
ErrorDocument 403 error/403
ErrorDocument 404 error/404

<Files .htaccess>
order allow,deny
deny from all
</Files>

IndexIgnore *
Options All -Indexes

【问题讨论】:

    标签: http .htaccess url-rewriting errordocument


    【解决方案1】:

    怎么样

    Options +FollowSymLinks
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    #Other options
    ErrorDocument 403 error/403
    ErrorDocument 404 error/404
    
    #Error
    RewriteRule    ^error/([0-9]{3})/?$    index.php?page=error-$1 [NC,L]
    

    所以你首先指定你的自定义错误页面,然后重写他们的(seo-friendly)url...

    【讨论】:

      【解决方案2】:

      ErrorDocument 指令的参数可以是字符串、内部 URI 或外部 URI。您在此处的“error/404”变体被解释为字符串,因此 Apache 只是将其输出,就好像它是硬编码的错误消息一样。这不是我们现在想要的。

      现在,如果我们在它前面加上一个斜杠,“/error/404”将是一个本地重定向,但相对于 DocumentRoot 是本地的。因此,如果我们在http://example.com/testit/.htaccess 有.htaccess 文件,并且我们请求http://example.com/testit/something.txt,则将加载文档http://example.com/error/404。为了避免这种情况,只需在它前面加上子目录的路径( /testit/error/404 ),或者以其他方式使用 DocumentRoot。但是,请注意,它已经是软重定向:浏览器的地址栏会显示用户输入的 URI,因此无需将 ErrorDocument 的 URI 设置为漂亮。只是

      ErrorDocument 404 /path-to-my-folder/index.php?page=error-404
      

      将给出一个非常好的行为:当我们请求找不到的 example.com/something.txt 时,地址栏将继续显示 example.com/something.txt,而 index.php 的内容?page=error-404 将被提供。

      但是如果你真的想在浏览器的地址栏中显示 /error/404,给 ErrorDocument 一个绝对 URI 作为参数,比如ErrorDocument 404 http://my-server/my-path/error/404 这将强制浏览器硬重定向到那个文档;请求将被 mod_rewrite 规则捕获,并提供 index.php?page=error-404 的内容,而地址栏将继续显示 /error/404。这种方法的缺点是您必须手动设置 404 响应标头,并且您不知道原始请求是什么。它也可能会迷惑机器人,也会迷惑访客,因为这不是他们所期望的行为。

      我的建议是,将其设为相对链接,如下所示:

      #Rewrite options
      RewriteEngine On    # Turn on the rewriting engine
      
      #Other options
      ErrorDocument 403 /path-to-my-subfolder/index.php?page=error-403
      ErrorDocument 404 /path-to-my-subfolder/index.php?page=error-404
      
      IndexIgnore *
      Options All -Indexes
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-11-29
        • 1970-01-01
        • 2014-03-29
        • 2016-11-30
        • 2010-12-16
        • 2017-10-19
        • 1970-01-01
        相关资源
        最近更新 更多