Redirect subdirectory of old domain to root folder of new domain via htaccess

谁说胖子不能爱 提交于 2019-12-11 04:24:45

问题


I have gone through several domain iterations on a wordpress blog and want to get old links redirected to the new domain. Initially the blog was in

olddomain.com/blog/

Now the blog is at the root level of

 newdomain.com

The old domain is set up as a domain alias of the new domain, it is not forwarded.

If anyone visits the site from

 olddomain.com/blog/

I need that to redirect to

 newdomain.com/

Thanks for your help.


回答1:


  1. Just create your .htaccess file and put it in the folder where you want to redirect from e.g put it in the /blog folder.

  2. Add the following lines to your .htaccess file.

    RewriteEngine On

    RewriteCond %{SERVER_PORT} 80

    RewriteCond %{REQUEST_URI} blog

    RewriteRule ^(.*)$ http://www.newside.com/newblog/$1 [R,L]




回答2:


If you are running an apache web server you can just do it vía a 301 redirect in a .htaccess file

  1. Go to you olddomain.com FTP root
  2. Create or edit .htaccess file
  3. Add following lines

    RewriteEngine On
    RewriteRule ^blog/* http://www.newdomain.com/ [R=301,L]
    

But this just redirect anything to the new domain, if you want to be "SEO friendly" (conserve canonical URLs) you will have to define a bit complex rule, take a look here: http://www.ksl-consulting.co.uk/301-redirect-examples.html




回答3:


If you're not strictly limited to .htaccess and have the option to modify VirtualHost configuration for olddomain.com you can add this to the configuration block:

RewriteEngine on
RedirectMatch 301 ^/blog/(.*) http://newdomain.com/$1

This assumes newdomain.com is a separate VirtualHost and we're leveraging that for domain matching to prevent a redirect loop when the source is in a common folder.



来源:https://stackoverflow.com/questions/16343447/redirect-subdirectory-of-old-domain-to-root-folder-of-new-domain-via-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!