301 重定向有关动态 URL 和多个 ID 的帮助

发布于 2024-08-10 23:12:59 字数 1027 浏览 15 评论 0原文

我刚刚对 www.wildchildclothes.com 进行了重新设计,它具有新的 URL 结构。旧网站在 Zen Cart 上运行,并且 URL 结构很糟糕。以下是我想要重定向的一些示例:

旧:www.wildchildclothes.com/page.html?chapter=1&id=21 新:www.wildchildclothes.com

和...

旧:www.wildchildclothes.com/index.php?main_page=faq_info&fcPath=0&faqs_id=13 新:www.wildchildclothes.com/customer-service.html

以下是我的 .htaccess 中实现此目的的内容:

RewriteEngine On
RewriteBase / 
RewriteCond %{QUERY_STRING} ^([^&]*&)*chapter=1(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*id=21(&|$)
RewriteRule ^page\.html http://www.wildchildclothes.com? [L,R=301]
RewriteCond %{QUERY_STRING} ^([^&]*&)*main_page=faq_info(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*fcPath=0(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*faqs_id=13(&|$)
RewriteRule ^index\.php$ http://www.wildchildclothes.com/customer-service.html? [L,R=301]

但这根本不起作用 - 我只会被发送到我的 404 页面,而不是开始重定向。任何人都可以透露一些信息吗?完整的 .htaccess 发布在下面。

非常感谢, 约拿

I just did a redesign for www.wildchildclothes.com which has a new URL structure. The old site ran on Zen Cart and had a bad URL structure. Here are some examples of what I want to redirect from:

OLD: www.wildchildclothes.com/page.html?chapter=1&id=21
NEW: www.wildchildclothes.com

and...

OLD: www.wildchildclothes.com/index.php?main_page=faq_info&fcPath=0&faqs_id=13
NEW: www.wildchildclothes.com/customer-service.html

Here is what's in my .htaccess to achieve this:

RewriteEngine On
RewriteBase / 
RewriteCond %{QUERY_STRING} ^([^&]*&)*chapter=1(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*id=21(&|$)
RewriteRule ^page\.html http://www.wildchildclothes.com? [L,R=301]
RewriteCond %{QUERY_STRING} ^([^&]*&)*main_page=faq_info(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*fcPath=0(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*faqs_id=13(&|$)
RewriteRule ^index\.php$ http://www.wildchildclothes.com/customer-service.html? [L,R=301]

But this doesn't work at all - I only get sent to my 404 page instead of begin redirected. Can anyone shed some light? The full .htaccess is posted below.

Thanks much,
Jonah

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

杀手六號 2024-08-17 23:12:59

上述解决方案都不适合我,但这确实有效:

将 www.wildchildclothes.com/page.html?chapter=1&id=21 重定向到 www.wildchildclothes.com:

RewriteCond %{query_string} chapter=1&id=21
RewriteRule (.*) http://www.wildchildclothes.com/? [R=301,L]

重定向 www.wildchildclothes.com/index.php ?main_page=document_product_info&products_id=280 至 www.wildchildclothes.com:

RewriteCond %{query_string} main_page=document_product_info&products_id=280
RewriteRule (.*) http://www.wildchildclothes.com/? [R=301,L]

将 www.wildchildclothes.com/index.php?main_page=faq_info&fcPath=0&faqs_id=9 重定向至 www.wildchildclothes.com/customer-service.html :

RewriteCond %{query_string} main_page=faq_info&fcPath=0&faqs_id=9
RewriteRule (.*) http://www.wildchildclothes.com/customer-service.html? [R=301,L]

我确信有更好的方法可以做到这一点,但这对我有用,而 .htaccess 只是让我头疼,所以我不再进一步挖掘。

我希望这对其他人有帮助!

  • 约拿

None of the above solutions worked for me, but this does:

To redirect www.wildchildclothes.com/page.html?chapter=1&id=21 to www.wildchildclothes.com:

RewriteCond %{query_string} chapter=1&id=21
RewriteRule (.*) http://www.wildchildclothes.com/? [R=301,L]

To redirect www.wildchildclothes.com/index.php?main_page=document_product_info&products_id=280 to www.wildchildclothes.com:

RewriteCond %{query_string} main_page=document_product_info&products_id=280
RewriteRule (.*) http://www.wildchildclothes.com/? [R=301,L]

To redirect www.wildchildclothes.com/index.php?main_page=faq_info&fcPath=0&faqs_id=9 to www.wildchildclothes.com/customer-service.html:

RewriteCond %{query_string} main_page=faq_info&fcPath=0&faqs_id=9
RewriteRule (.*) http://www.wildchildclothes.com/customer-service.html? [R=301,L]

I'm sure there are better ways to do this but this works for me and .htaccess just gives me a headache so I'm not digging any further.

I hope this helps someone else out there!

  • Jonah
故事和酒 2024-08-17 23:12:59

如果您使用 PHP 进行重定向,会更容易、更好。例如,您可以将此类请求重写为 PHP 脚本,该脚本分析 URL 参数并重定向到正确的页面。也许是这样的:

// old-to-new.php
$rules = array(
    array(
        array('chapter' => '0', 'id' => '23'),  // old URL parameters
        '/customer-service.html'                // new URL
    ),
    // …
);
foreach ($rules as $rule) {
    if (array_intersect_assoc($rule[0], $_GET) == $rule[0]) {
        header('Location: http://example.com'.$rule[1], true, 301);
        exit;
    }
}
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
exit;

以及相应的RewriteRule

RewriteRule ^page\.html$ old-to-new.php [L]

It would be easier and far better if you use PHP for the redirect. You could, for example, rewrite such requests to a PHP script that analyzes the URL arguments and redirects to the correct page. Maybe something like this:

// old-to-new.php
$rules = array(
    array(
        array('chapter' => '0', 'id' => '23'),  // old URL parameters
        '/customer-service.html'                // new URL
    ),
    // …
);
foreach ($rules as $rule) {
    if (array_intersect_assoc($rule[0], $_GET) == $rule[0]) {
        header('Location: http://example.com'.$rule[1], true, 301);
        exit;
    }
}
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
exit;

And the corresponding RewriteRule:

RewriteRule ^page\.html$ old-to-new.php [L]
青衫负雪 2024-08-17 23:12:59

您希望在查询参数中允许使用多个字符。将 [^&]& 更改为 [^&]*&

RewriteCond %{QUERY_STRING} ^([^&]*&)*chapter=1(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*id=21(&|$)
RewriteRule ^page\.html$ http://www.wildchildclothes.com? [L,R=301]

RewriteCond %{QUERY_STRING} ^([^&]*&)*main_page=faq_info(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*fcPath=0(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*faqs_id=12(&|$)
RewriteRule ^index\.php$ http://www.wildchildclothes.com? [L,R=301]

You want to allow more than one character in the query parameters. Change [^&]& to [^&]*&:

RewriteCond %{QUERY_STRING} ^([^&]*&)*chapter=1(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*id=21(&|$)
RewriteRule ^page\.html$ http://www.wildchildclothes.com? [L,R=301]

RewriteCond %{QUERY_STRING} ^([^&]*&)*main_page=faq_info(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*fcPath=0(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*faqs_id=12(&|$)
RewriteRule ^index\.php$ http://www.wildchildclothes.com? [L,R=301]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文