4

I just created a new subdomain for my blog so blog.example.com - it used to be www.example.com/blog so I am now trying to write 301 redirects from all my old /blog posts to the new blog.example.com URLs. I have placed the following code in my htaccess:

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

However, do I put it after the # END Wordpress in the htaccess or am I missing something? Where in the htaccess file do I put this code or is it even the correct code?

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

OR Do I need to write out

Redirect 301 /blog/example-post http://blog.example.com/example-post

Any help would be appreciated, thanks!

  • Is your domain already canonicalised? ie. Can it only be accessed by the www subdomain (and the blog subdomain)? Does your blog subdomain point to the same document root as your main domain? Have you already changed the URL structure in WordPress itself? Have you actually tried this and what was the result? – MrWhite Oct 22 '16 at 9:58
5

...do I put it after the # END Wordpress in the htaccess or am I missing something?

No. Any external redirect should come before the # BEGIN WordPress code block. ie. Before WP internally rewrites the request.

If you placed it after the WP code block it will simply be ignored, since WP will have already routed the request.

Otherwise your mod_rewrite redirect looks OK, assuming your old /blog/ URL always has a trailing slash (to the "blog root"), the blog subdomain points to the same place on the filesystem as your main domain and you have already canonicalised the domain (ie. it can only be accessed via the www subdomain).

 Redirect 301 /blog ....

You should avoid mixing RewriteRule (ie. mod_rewrite) and Redirect (ie. mod_alias) directives. Since these two directives belong to different modules they execute at different times during the request and you can end up with confusing conflicts if you are not careful. So, since you are already using mod_rewrite (WordPress directives) then you should stick to using mod_rewrite for any redirects.

It wouldn't matter were you placed the Redirect in your config file - it would still execute after the mod_rewrite directives.

2

You want something like this using a mod_alias

Redirect permanent /blog http://blog.example.com/

The keyword permanent causes Apache to send an HTTP status of 301 Moved Permanently instead of 302 Found.

To use your method you can try this

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^blog/(.*)$ http://blog.example.com/$1 [L,QSA,R=301]

RewriteCond %{HTTP_HOST} ^blog\.example\.com$
RewriteCond %{REQUEST_URI} !^blog/
RewriteRule ^(.*)$ /blog/$1 [L,QSA]
  • 1
    Other than making www. optional, I'm not seeing how your rewrite rule is different than the ones that are not working in the question. I think the problem may be related to the placement position in .htaccess rather than due to the rule itself. – Stephen Ostermiller Oct 22 '16 at 8:56
  • It appears that your logic is backwards from what the OP is looking for... /blog to blog.example.com. Sorry. Cheers!! – closetnoc Oct 22 '16 at 15:16
-3

I had same problem, site was very very slow.

Changed [L,R=301] to [R=301,L] and problem solved.

  • 2
    "same problem"? The question is not about site speed. [L,R=301] and [R=301,L] are indentical, so it wasn't that that solved your problem. (?) – MrWhite Oct 23 '16 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.