【问题标题】:htaccess - redirect with condition not working on localhosthtaccess - 重定向条件不适用于本地主机
【发布时间】:2019-04-05 08:24:43
【问题描述】:

我在 htaccess 中有这个重定向

我想重定向

http://localhost/www/foo/bar.php?city=new_york&data=4156

http://localhost/www/foo/new_york/?&data=4156

我已经创建了这个 htaccess:

RewriteEngine on

RewriteCond %{REQUEST_URI}  bar\.php$
RewriteCond %{QUERY_STRING} ^city\=([^&]+)(.*)$
RewriteRule ^(.*)/bar.php$ http://%{HTTP_HOST}/$1/%1/?%2 [L,R=301]

但这不适用于本地主机。 URL 保持不变,旧的。我已经用https://htaccess.madewithlove.be/ 测试了这些规则,它们似乎按预期工作。那么这只是 localhost 的问题还是 htaccess 错误?

【问题讨论】:

  • RewriteCond %{QUERY_STRING} (?:^|&)city=([^&]+)(?:&|$)
  • 您的 .htaccess 在哪里?是在/foo/ 目录里面吗?
  • @anubhava 是的,它在 /foo/ 目录中

标签: .htaccess localhost


【解决方案1】:

您可以在foo/.htaccess 中使用此规则:

RewriteEngine on

RewriteCond %{QUERY_STRING} ^city=([^&]+)&(.*)$ [NC]
RewriteRule ^bar\.php$ /foo/%1/?%2 [L,R=301]

请注意,这将重定向到 http://localhost/www/foo/new_york/?data=4156,因为在 URL 中的 ? 之后紧跟 & 是没有意义的。

【讨论】:

    猜你喜欢
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 2018-05-06
    • 2014-10-01
    • 2012-12-13
    相关资源
    最近更新 更多