简体   繁体   English

重定向htaccess中的多个链接

[英]Redirect multiple links in htaccess

I'm having a problem with redirects, we made a new website and the blog from the old website needs to be redirected to the the new blog. 我遇到重定向问题,我们建立了一个新网站,旧网站上的博客需要重定向到新博客。 Every blogpost needs to be redirected, there are more than 200 of them. 每个博客帖子都需要重定向,其中有200多个。 So this can't be done for every link 所以每个链接都无法做到这一点

This was the old URL: 这是旧网址:

example.com/nl/blog/easy-car-shopping

This should be the new URL: 这应该是新的URL:

blog.example.com/easy-car-shopping/

So example.com/nl/blog/ should be redirected to blog.example.com/ but so far I fail to get this to work. 因此,应将example.com/nl/blog/重定向到blog.example.com/但到目前为止我无法使其工作。

Could anyone help me out here? 谁能帮到我这里?

You can create a .htaccess file in the root directory of your example.com site. 您可以在example.com站点的根目录中创建.htaccess文件。

RewriteEngine on
RewriteBase / 

#if not already blog.example.com/
RewriteCond %{HTTP_HOST} !^blog\.example\.com$ [NC] 
#if request is for blog/, go to blog.example.com
RewriteRule ^blog/$ http://blog.example.com [L,NC,R=301] 

If you would delete the /nl/ you should include it on the regular expresion. 如果要删除/nl/ ,则应将其包含在常规表达中。

It should work for you, hope it helps! 它应该适合你,希望它有所帮助!

In the .htaccess file at /nl/blog/.htaccess (assuming /nl/blog is a physical subdirectory), try the following using mod_rewrite: /nl/blog/.htaccess.htaccess文件中(假设/nl/blog是一个物理子目录),请使用mod_rewrite尝试以下操作:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://blog.example.com/$1/ [R=302,L]

As in your example, this assumes the source URL does not contain a trailing slash. 如您的示例所示,这假定源URL不包含尾部斜杠。 A trailing slash is then appended on the substitution (target URL). 然后在替换 (目标URL)上附加尾部斜杠。

By placing these directives in the subdirectory .htaccess file and not the document root, it avoids unnecessary processing on the main site. 通过将这些指令放在子目录.htaccess文件而不是文档根目录中,可以避免在主站点上进行不必要的处理。

Change the 302 (temporary) redirect to a 301 (permanent) redirect when you are sure it's working OK. 当您确定它正常工作时,将302 (临时)重定向更改为301 (永久)重定向。 301s are cached hard by the browser so can make testing problematic. 301s被浏览器缓存,因此可能会使测试出现问题。

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

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