问题原文
原帖链接 · 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=1andc=2and from the URL withoutcin it tohttps://www.example.com/product/product_slug/? Or should I just redirect fromindex.php?pr=1to 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.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
