English 中文(简体)
301 Redirect Help with Dynamic URL s and Multiple Id s
原标题:

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

最佳回答

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
问题回答

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]

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]




相关问题
Ignore symlinks in clean URL s in .htaccess

Example URL: example.com/user /user is both a symlinked directory and a valid URL to content on my site. I user Horde Routes to request the content and all requests to the site go through index.php. ...

.htaccess File Options -Indexes on Subdirectories

I have the following .htaccess line, simple no indexes on root. Options -Indexes What do we add so it propagates to any sub directory instead of having to create one file for each? One .htaccess on ...

Mod Rewrite ignoring -d

After a day or two, I am still fighting with Mod Rewrite. It seems that 60% of my development time is spent battling the server. My current URL structure is such that all http://example.com/xyz and ...

ErrorDocument 404 Not Sending Referrer Information: PHP

I m using a standard htaccess ErrorDocument 404 to redirect users to a new page. In order to customize the 404, I need to know their referrer information (from the page they Tried to visit). $...

Find all htaccess files on server [closed]

I m looking for a Linux command to go through all the directories on my server and find all .htaccess files. The output would be a list of all those files with full path, creation/modification date ...

301 Redirect Help with Dynamic URL s and Multiple Id s

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:...

热门标签