【问题标题】:.htaccess with two parameters redirects to a 404 page带有两个参数的 .htaccess 重定向到 404 页面
【发布时间】:2015-07-26 17:23:57
【问题描述】:

我想将/index.php?id=1&time=10 重定向到/first-10

.htaccess

RewriteEngine On
RewriteCond %{QUERY_STRING} id=1&time=10
RewriteRule ^index\.php$ /first-10/? [L,R=301]

当我访问 /index.php?id=1&time=10 时,会重定向到 /first-10/,但这是一个 404 页面。请注意最后一个斜线。

问题出在哪里?

【问题讨论】:

    标签: apache .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    您需要在内部将其重写回原来的旧格式。
    您必须使用THE_REQUEST 来避免无限循环

    RewriteEngine On
    
    RewriteCond %{THE_REQUEST} \s/(?:index\.php)?\?id=1&time=10\s [NC]
    RewriteRule ^ /first-10? [R=301,L]
    
    RewriteRule ^first-10$ /index.php?id=1&time=10 [L]
    
    • http://example.com/index.php?id=1&time=10http://example.com/?id=1&time=10 都将重定向到 http://example.com/first-10
    • http://example.com/first-10 将在内部将其重写为 /index.php?id=1&time=10

    【讨论】:

    • 我很确定他不需要担心无限循环,因为第一个重定向是 301。此外,他需要修改脚本以处理该 URI,而不需要查询字符串.
    • 由于 OP 需要重写,如果不使用THE_REQUEST,他将面临循环问题。是的,这是强制性的(否则,他会得到 404,因为它不存在)
    • 这可行,但会重定向到/first-10?id=1&time=10
    • 确保在第一个/first-10 之后保留? RewriteRule
    • 啊,我明白他为什么需要 RewriteCond 指令了。
    【解决方案2】:

    如果问题真的是.htaccess,结果页面是正确的并且调用了重定向,我在修补:

    “?”字符 betwhen first-10/ 和 [L,R= ??? 还是前 10 个之后的 / ???

    【讨论】:

      猜你喜欢
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      • 2013-01-31
      • 1970-01-01
      • 2016-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多