Swadesh Behera
Swadesh Behera

Reputation: 927

Redirecting all sub-sub pages to another subpage using htaccess

I've a older site running in Apache Server which is already indexed in Google. I wish to redirect all those indexed links to my new site (As the older pages are not existing any more.)

So i wish to redirect all my sub-sub pages to my new root page

I've pages like follows

http://itdost.com/answer-now/Aerobics
http://itdost.com/answer-now/HTML
http://itdost.com/answer-now/Culture

I use the following redirect code for each one

Redirect 301 /answer-now/Engineering http://www.itdost.com/questions/
Redirect 301 /answer-now/Food http://www.itdost.com/questions/
Redirect 301 /answer-now/ASP http://www.itdost.com/questions/

But as the site structure is big, i wish to do it in a single line instead of writing a line for each redirect

Some thing like the following.

Redirect 301 /answer-now/% http://www.itdost.com/questions/

But the above code does not seems to work

Upvotes: 5

Views: 4349

Answers (3)

MaxiGui
MaxiGui

Reputation: 6348

The same question asked by the same person, but the answer accepted is different, it is good to share it here too. Here: https://serverfault.com/questions/487006/redirecting-all-sub-pages-to-another-subpage-using-htaccess?newreg=b825d1fee10b48cba7366e740c21b770

Please give credit to Laetitia on original post:

RedirectMatch 301 /answer-now/.* http://www.itdost.com/questions

I would add that if you need to rewrite multiple subsub /answer-now/Food/burger to http://www.itdost.com/questions/, you will need to add \/.* at the end.

RedirectMatch 301 /answer-now/.*\/.* http://www.itdost.com/questions

Upvotes: 0

anubhava
anubhava

Reputation: 785128

In order to use regex better to use mod_rewrite which is more powerful than mod_alias.

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^answer-now(/.*|)$ http://www.itdost.com/questions/? [L,NC,R=301]

Upvotes: 2

kjetilh
kjetilh

Reputation: 4976

Try this:

RedirectMatch 301 ^/answer-now/ http://www.itdost.com/questions/

Upvotes: 1

Related Questions