简体   繁体   中英

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.

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:

使用新域重定向前两个段来重定向旧域

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