大网数据广告活动
网站运营与故障问答

How to bulk redirect in htaccess from old site with parameters

· 52CE 我爱测

I'm currently updating an old website, but I don't know much about htaccess redirects. I made a script to get all the pages of the website and here are some examples: For pages: Redirect 301 /index.php?p=1 https://www.example.com…

问题原文

原帖链接 · ErXoR · CC BY-SA 4.0

I'm currently updating an old website, but I don't know much about htaccess redirects. I made a script to get all the pages of the website and here are some examples:

For pages:

Redirect 301 /index.php?p=1 https://www.example.com/page_slug/

For categories I did the same:

Redirect 301 /index.php?c=1 https://www.example.com/cat_slug/

Now for the product pages. There are some products that are in multiple categories. For example product 1 is visible in category 1 and 2:

index.php?c=1&pr=1
index.php?c=2&pr=1

Those products have a canonical to

index.php?pr=1

For SEO purposes, should I redirect from c=1 and c=2 and from the url without c in it to https://www.example.com/product/product_slug/? Or should I just redirect from index.php?pr=1 to the new page?

If I have to redirect from all the categories, is there a way to write it without having to write every category for the same product (it's written with a script, so it's not that much work, but there will be a lot of lines in the htaccess file)?

Code already in the file (in an update folder):

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /update/

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /update/index.php [L]
</IfModule>

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

For categories I did the same:

Redirect 301 /index.php?c=1 https://www.example.com/cat_slug/

I'm not sure whether you are implying these redirects work OK? But this cannot possibly work... you can't match the query string with a the Redirect directive. You need to use mod_rewrite and a condition that checks against the QUERY_STRING server variable. For example:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^c=1$
RewriteRule ^(index\.php)?$ /cat_slug/ [QSD,R=301,L]

The QSD flag (Apache 2.4+) is required to remove the original query string from the redirected request, otherwise it is passed through unchanged. (On Apache 2.2 you would need to append a ? to the end of the substitution string instead.)

And the same for pages.

For SEO purposes, should I redirect from c=1 and c=2 and from the URL without c in it to https://www.example.com/product/product_slug/? Or should I just redirect from index.php?pr=1 to the new page?

If all variations are indexed and/or being linked to then you need to redirect from all variations. However, you can do all that in a single rule, for example:

RewriteCond %{QUERY_STRING} ^(c=[12]&)?pr=1$
RewriteRule ^(index\.php)?$ /product-page/ [QSD,R=301,L]

If I have to redirect from all the categories, is there a way to write it without having to write every category for the same product

You only have what is available in the requested URL. So, if you can determine the target URL simply by looking at the source URL then yes, you can effectively "group" these rules together. However, in the examples you've posted, this is not the case.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2019-05-03)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。