Daniel Garcia Sanchez
Daniel Garcia Sanchez

Reputation: 2344

htaccess rewriterule to redirect to new domain

I need to redirect all articles from an old domain to a new domain. Old domain articles (non wordpress site) slugs are like:

www.olddomain.com/CATEGORY_NAME/ID_ARTICLE/ARTICLE_SLUG

New domain articles (wordpress site) slugs are like:

www.newdomain.com/ARTICLE_SLUG

So, I need htaccess rules to redirect from old domain to the new one, that is, removing the two first segments of the old domain (CATEGORY_NAME and ID_ARTICLE).

At the moment, I have this in the htaccess file:

RewriteRule ^(.*)$ http://www. newdomain .com/$1 [R=301,L]

But it makes redirection with all urls.

Upvotes: 0

Views: 471

Answers (1)

sepehr
sepehr

Reputation: 18445

Try this:

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{HTTP_HOST} ^www\.olddomain\.com
RewriteRule ^(.*)/(.*)/(.*)$ http://www.newdomain.com/$3 [L,R=301,QSA]

Lose the [L] flag, if there are further rewrites ahead in the file.

I tested it online and it seems to be working as you want:

Redirect old domain with new domain dropping the first two segments

Upvotes: 2

Related Questions