【问题标题】:mod_rewrite error 404 if .php如果 .php,则 mod_rewrite 错误 404
【发布时间】:2010-11-06 12:57:13
【问题描述】:

我重写了我的网址以方便用户使用。例如,我有一个名为 user.php 的页面,我将其重写为 /user。但是用户仍然可以使用 user.php。如果他们请求带有 .php 扩展名的页面,我可以重定向到 404 吗?

Options -MultiViews +FollowSymlinks -Indexes
RewriteEngine on


RewriteRule ^user/([0-9]+)$ user.php?id=$1 [L,QSA]

谢谢。

【问题讨论】:

    标签: php mod-rewrite


    【解决方案1】:
    RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/
    RewriteRule ^.*$ - [R=404,L]
    

    【讨论】:

    • 可行,但如果我在 user.php? 之后添加问号,我仍然可以访问该页面。也许我可以为此做点什么?
    • R=404 是允许的,但是 apache 只发送 Location 标头是 http-code 在 300 范围内。
    • 您应该添加 [nc],否则页面将加载大写扩展名
    【解决方案2】:

    我认为这应该可以解决问题:

    RewriteRule (.*)\.php$ path/to/your/404file [L]
    

    【讨论】:

    • 所以我需要为我的所有页面添加规则?
    【解决方案3】:

    301 redirect可能更适合此任务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2014-02-24
      相关资源
      最近更新 更多