简体   繁体   中英

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. 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:

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.

Could anyone help me out here?

You can create a .htaccess file in the root directory of your example.com site.

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.

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:

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. A trailing slash is then appended on the substitution (target URL).

By placing these directives in the subdirectory .htaccess file and not the document root, it avoids unnecessary processing on the main site.

Change the 302 (temporary) redirect to a 301 (permanent) redirect when you are sure it's working OK. 301s are cached hard by the browser so can make testing problematic.

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