简体   繁体   中英

301 redirects not working for query strings

I've .htaccess file which contains some 301 redirects. See the .htaccess code below. The third 301 redirect works fine. But the first and second redirects are producing 404 Page not found result. I've searched over SO and google and found some solutions. But none of them are working.

Following is my .htaccess file:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Redirect 301 /page.php?page=8  http://www.mydomain.com/about-us/

Redirect 301 /page.php?page=5  http://www.mydomain.com/contact-us/

Redirect 301 /testimonial.php http://www.mydomain.com/testimonials/

Please suggest.

I think that if you do it like this, it could work (according to this http://httpd.apache.org/docs/current/en/mod/mod_alias.html#redirect ):

Redirect 301 /page.php http://www.mydomain.com/contact-us/

But it's not doing what you expected for. So I suggest these rules

RewriteCond %{QUERY_STRING} (^|&)id=8(&|$)
RewriteRule ^page\.php http://www.mydomain.com/about-us/ [R=301,L]

RewriteCond %{QUERY_STRING} (^|&)id=5(&|$)
RewriteRule ^page\.php http://www.mydomain.com/contact-us/ [R=301,L]

I really don't know if that's the best way.

I've tried many was found here in SO and google.com; Nothing worked. Finally, I used a different approach. I've created page.php file and there I used following code:

$page = $_GET['page'];
switch ($page) {
case 6:
$link = 'http://webindream.com/about-us/';
break;

case 7:
$link = 'http://webindream.com/contact-us';
break;

default: 
$link = 'http://webindream.com/';


}

header("Location: $link", true, 301);

Hope this will help someone to get rid of .htaccess and redirect smoothly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM