【问题标题】:How do I properly rewrite my URLs to give a proper 404 page?如何正确重写我的 URL 以提供正确的 404 页面?
【发布时间】:2019-02-20 07:15:39
【问题描述】:

我当前的 .htaccess mod_rewrite 配置有问题。

这是我当前的代码:

RewriteRule ^([^/]*)/([^/]*)\/$ /game.php?id=$1&title=$2 [L]
RewriteRule ^([^/]*)\/$ /index.php?page=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\/$ /category.php?cid=$1&page=$2&title=$3 [L]

上面的代码正确地重写了我的 URL。但是,每当我在浏览器中输入一个不存在的目录时,例如http://www.example.com/non-existent-directory/,我的主页就会出现,而不是我设置的 404 页面。

另一个注意事项,尾部的斜线似乎有很大的不同。如果我从 URL 中删除尾部斜杠,即 http://www.example.com/non-existent-directory,则 404 页面会正确显示。如果我添加它,我的主页会出现,而不是应有的 404 页面。

有谁知道我需要做什么才能让我的 URL 重写,但在我键入不存在的目录时也会显示 404 页面?

提前谢谢你。

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    这是ErrorDocument 方向;同时也可以去掉尾部的斜杠:

    # Rewrites
    <IfModule mod_rewrite.c>
    
        RewriteEngine On
        RewriteBase /
    
        # remove trailing slash
        RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
    
        ...
    
    </IfModule>
    
    # HTTP Errors
    ErrorDocument 404 /index.php?controller=frontend&method=error&id=404
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 2020-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多