简体   繁体   English

通过htaccess将旧域的子目录重定向到新域的根文件夹

[英]Redirect subdirectory of old domain to root folder of new domain via htaccess

I have gone through several domain iterations on a wordpress blog and want to get old links redirected to the new domain. 我在wordpress博客上经历了几次域迭代,并希望将旧链接重定向到新域。 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. Just create your .htaccess file and put it in the folder where you want to redirect from eg put it in the /blog folder. 只需创建您的.htaccess文件,并将其放在您要从中重定向的文件夹中即可,例如,将其放在/ blog文件夹中。

  2. Add the following lines to your .htaccess file. 将以下行添加到您的.htaccess文件。

    RewriteEngine On RewriteEngine开

    RewriteCond %{SERVER_PORT} 80 RewriteCond%{SERVER_PORT} 80

    RewriteCond %{REQUEST_URI} blog RewriteCond%{REQUEST_URI}博客

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

If you are running an apache web server you can just do it vía a 301 redirect in a .htaccess file 如果您正在运行apache网络服务器,则可以通过.htaccess文件中的301重定向来进行操作

  1. Go to you olddomain.com FTP root 转到您的olddomain.com FTP根目录
  2. Create or edit .htaccess file 创建或编辑.htaccess文件
  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 但这只是将任何内容重定向到新域,如果您想成为“ SEO友好”(保存规范的URL),则必须定义一些复杂的规则,请在此处查看: http : //www.ksl-consulting.co .UK / 301重定向-examples.html

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: 如果您不限于.htaccess并可以选择为olddomain.com修改VirtualHost配置, olddomain.com可以将其添加到配置块中:

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. 假设newdomain.com是一个单独的VirtualHost并且我们正在利用它进行域匹配,以防止源位于公共文件夹中时发生重定向循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM