php - 301 redirection to changed urls

459

I need to change the domain name of my site and I will use permanent redirection 301. I found that this setup on .htaccess will do the job:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://www.new-example.com/$1 [L,R=301,NC]

But I also need to change all urls structure from http://www.new-example.com/old/this-is-url1.html to http://www.new-example.com/this-is-url1.html.

Rule must effect only urls with directoryold on their alias: www.new-example.com/old/...

And redirect them to urls without directoryold: www.new-example.com/

Can you please show me what redirect rule I must use on .htaccess to make this happen?

41

Answer

Solution:

Replace your shown code with this rule:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(?:old/?)?(.*)$ http://new-example.com/$1 [L,R=301,NC,NE]

Clear your browser cache before testing this change.

People are also looking for solutions to the problem: php - WordPress parent-child menu HTML

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.