【问题标题】:Redirect www.example.com/some-path/example/ to www.example.com/some-path/?ABC=123将 www.example.com/some-path/example/ 重定向到 www.example.com/some-path/?ABC=123
【发布时间】:2021-09-16 09:54:07
【问题描述】:

我正在尝试重定向这样的路径:从 www.example.com/some-path/sg/www.example.com/some-path/

但是我需要一些方法来识别这种流量,比如一个参数,理想情况下:www.example.com/some-path/?ls=sg。这可以使用 htaccess/mod_rewrite 吗?

我试过的是:

RedirectMatch 301 /sg/(.*) /$1?ls=SG7

【问题讨论】:

  • 请在您的问题中分享您尝试过的 htaccess 规则文件,谢谢。
  • 我试过:RedirectMatch 301 /sg/(.*) /$1?ls=SG7 ..但这只是重定向到路由,但它确实包含参数,所以不会太远。不过,我只需要保留其余的路径。
  • sg 参数是否总是固定在任何类型的 url 中传递?请确认一次,因为您正在重定向到 url www.example.com/some-path/,它不会在 url 中,因此 htaccess 不会知道它是否在前端隐藏了一次。请确认一次。
  • 不,sg 不在所有网址中,只是一些。所以当它存在时,删除它并传递一个参数,这样我就可以在它后面做其他事情
  • 好的,所以 url www.example.com/some-path/?ls=sg 你在浏览器中点击?你想把它改成www.example.com/some-path/sg/吗?请确认一次

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


【解决方案1】:

根据您显示的尝试,请尝试遵循 htaccess 规则文件的示例。 确保将它们放在 htaccess 规则文件的顶部。

确保在测试您的 URL 之前清除您的浏览器缓存。

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
##To look for sg here.
RewriteCond %{THE_REQUEST} sg [NC]
RewriteRule ^(.*)/(.*)/?$ /$1/?ls=$2 [R=301,NE,L]

【讨论】:

  • 这几乎可行,谢谢。当它之前有超过 1 个目录时,只有一个问题。 IE。 www.example.com/some-path/another-path/sg/
  • @FullStackMedia,好的,你能不能测试一下我编辑的代码,让我知道它是怎么回事?干杯。
  • 抱歉,它快到了,但我仍然在重定向的 url 中保留 /sg/,我需要把它去掉,否则会导致错误。感谢您的所有帮助!
  • example.com/example/example/john-doe/sg 被重写为 example.com/example/example/john-doe/sg/?ls=sg 这几乎是正确的。我只需要从新 url 中删除 /sg/ 部分。
  • RewriteCond %{REQUEST_URI} sg RewriteRule ^((?:[^/]*)(?:/[^/]*))/(.*) /$1/$2?ls=sg [R=301,NE,L] 是我目前所拥有的。在重写条件下是否可以更具体并在执行规则之前查找/sg/?
猜你喜欢
  • 1970-01-01
  • 2021-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-11
  • 1970-01-01
  • 2017-10-26
相关资源
最近更新 更多