【问题标题】:301 htaccess redirects with 3 parameters to static urls301 htaccess 使用 3 个参数重定向到静态 url
【发布时间】:2023-03-10 00:28:01
【问题描述】:

我试图用 .htaccess 将几个带有 3 个参数的 url 重定向到不同的静态 url,但没有任何效果。

1.

http://olddomain.com/index.php?id_category=28&controller=category&id_lang=2

https://newdomain.com/page1/
http://olddomain.com/index.php?id_category=30&controller=category&id_lang=2

https://newdomain.com/page2/
http://olddomain.com/index.php

https://newdomain.com

我尝试了下面的代码,但http://olddomain.com/index.php 不去https://newdomain.com

RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301,NC] 

RewriteCond %{QUERY_STRING} ^id_category=28&controller=category&id_lang=2$
RewriteRule ^index.php$ https://newdomain.com/page1/? [R=301,L]

RewriteCond %{QUERY_STRING} ^id_category=30&controller=category&id_lang=2$
RewriteRule ^index.php$ https://newdomain.com/page2/? [R=301,L]

【问题讨论】:

  • 欢迎来到 SO,感谢分享您的代码。您如何传递类别值以将 URL 重写为 index.php 文件?请详细说明。

标签: .htaccess redirect http-status-code-301


【解决方案1】:

如果您从 id_lang= 变量中获取页面 ID,请尝试以下规则。请确保在测试您的 URL 之前清除您的浏览器缓存。

RewriteEngine ON
##Rules to redirect to link: https://newdomain.com/page1/ here.
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/index\.php\?id_category=28&controller=category&id_lang=(\d+)\s [NC]
RewriteRule ^ https://newdomain.com/page%1/? [NE,R=301,L]

##Rules to redirect https://newdomain.com/ here.
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/index\.php\s [NC]
RewriteRule ^ https://newdomain.com [NE,R=301,L]

【讨论】:

  • 下面的代码正在运行并将类别重定向到新页面。但不能将首页olddomain.com重定向到newdomain.comolddomain.com/index.php重定向到newdomain.com RewriteCond %{QUERY_STRING} ^id_category=28&controller=category&id_lang=2$ RewriteRule ^index.php$ newdomain.com/page1? [R=301,L] RewriteCond %{QUERY_STRING} ^id_category=30&controller=category&id_lang=2$ RewriteRule ^index.php$ newdomain.com/page2? [R=301,L]
  • @Chris,请告诉我我的解决方案是否对您有用?谢谢。
  • 实际上这条规则不能基于id_lang=参数,因为根据OP,这两种情况下都是2
【解决方案2】:

您需要先有特定的较长匹配项,然后有规则来删除 index.php 或域重定向:

RewriteEngine On

# specific redirects with index.php as optional match
RewriteCond %{QUERY_STRING} ^id_category=28&controller=category&id_lang=2$ [NC]
RewriteRule ^(index\.php)?$ https://newdomain.com/page1/? [R=301,L,NC]

RewriteCond %{QUERY_STRING} ^id_category=30&controller=category&id_lang=2$ [NC]
RewriteRule ^(index\.php)?$ https://newdomain.com/page2/? [R=301,L,NC]

# remove index.php and redirect to newdmain
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteRule ^(?:index\.php/?)?(.*)$ https://newdomain.com/$1 [L,R=301,NC,NE]

确保在测试此更改之前清除浏览器缓存。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-29
    • 2015-04-27
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多