【问题标题】:Query string in htaccess results in 404 errorhtaccess 中的查询字符串导致 404 错误
【发布时间】:2015-02-16 18:31:27
【问题描述】:

目标是重定向一个查询字符串,如:

mydomain.com/sheet.csv?z=abcdef

到其各自的文件夹,将查询字符串附加到相应文件所在的文件夹名称中,如下所示:

mydomain.com/charts/abcdef/sheet.csv

我尝试了以下方法,但我尝试的所有方法都会导致 404 错误。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^sheet.csv?z=([_0-9a-z-]+) /$1/sheet.csv [R] 

不起作用,导致 404 错误。

RewriteCond %{QUERY_STRING}     ^z=([_0-9a-z-]+)$    [NC]
RewriteRule sheet.csv chart/$1/sheet.csv [NC,L,R=301]

不起作用,导致 404 错误。

我该如何解决这个问题?


更新

错误似乎正在发生,因为它不是重定向而是寻找文件:
mydomain.com/sheet.csv 不存在也永远不会存在

那么为什么它没有重定向,我该如何解决呢?


更新 2

我做了一堆反复试验。我能够让它做点什么,只需要完成它的工作。

如果我这样做:

RewriteCond %{QUERY_STRING} ^z=([\w-]+) [NC]
RewriteRule ^ charts/%1/sheet.csv? [NC,L,R=301]

然后它重定向到 url mydomain.com/home/mydomai/public_html/mydomain/charts/abcdef/sheet.csv

我尝试了所有我能想到的可能组合,但仍然无法直接访问 URL
mydomain.com/charts/abcdef/sheet.csv

请帮忙谢谢!

【问题讨论】:

    标签: javascript .htaccess mod-rewrite web http-status-code-404


    【解决方案1】:

    是的,您无法匹配 RewriteRule 中的 QUERY_STRING。改用RewriteCond 并修复您的正则表达式模式:

    RewriteCond %{QUERY_STRING} (?:^|&)z=([^&]+) [NC]
    RewriteRule ^(sheet\.csv)/?$ /chart/%1/$1? [NC,L,R=302]
    

    确保将此规则放在其他规则之前。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-24
      • 1970-01-01
      • 2015-04-25
      • 2011-04-17
      • 2011-04-27
      • 2021-10-04
      • 1970-01-01
      相关资源
      最近更新 更多