apeee
apeee

Reputation: 13

Redirect all 404 errors excluding specific 404 urls to homepage using htaccess?

I am currently renovating my website which has 1000s of pages; which requires changes to urls.

  1. I want to 301 permanent redirect ALL 404 errors to website homepage say www.domain.com except some specific 404 URLs.
  2. And I want to 301 permanent redirect those specifc excluded 404 URLs (in no.1) to another URL.

I tried following code:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/about-us0.html [NC]
RewriteCond %{REQUEST_URI} !^/contact_us0.html [NC]
RewriteRule ^(.*)$ /$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^about-us0\.html$ "http\:\/\/www\.domain\.com\/about\.html" [R=301,L]

RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^contact-us0\.html$ "http\:\/\/www\.domain\.com\/contact\.html" [R=301,L]

But when I browse to www.domain.com/about-us0.html or www.domain.com/contact_us0.html; google chrome says that "This webpage has a redirect loop".

Is there something I am doing wrong? Any help will be appreciated.


For redirecting without carring query string:

http://domain.com/prod/species.php?action=3&file_id=30

to

http://domain.com/species-30.php

AND

http://domain.com/prod/species.php?action=3&file_id=101

to

http://domain.com/species-101.php

I did something like this (below) which is working fine. Is there any changes that requires in this?

RewriteCond %{QUERY_STRING} ^action=3&file_id=([0-9]+)$ [NC]
RewriteRule ^prod/species\.php$ /species-%1.php? [R=301,L]

Upvotes: 1

Views: 1753

Answers (1)

anubhava
anubhava

Reputation: 785481

Try these rules:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(about|contact).*?\.html [NC]
RewriteRule ^ / [R=301,L]

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^(contact|about)-us0\.html$ http://www.domain.com/$1.html [R=301,L,NC]

Upvotes: 0

Related Questions