Brian Lewis
Brian Lewis

Reputation: 13

htaccess - Redirect content inside subdomain, but not subdomain

I'm currently using:

RedirectMatch 301 http://blog.domain.com/(.*) /blog/article/$1

To redirect old links like: blog.domain.com/article-title to www.domain.com/blog/article/article-title

However, when I simply go to blog.domain.com, it's taking me to www.domain.com/blog/article.

This is an unintended side effect. I'd like blog.domain.com/ itself to redirect simply to www.domain.com/blog but the content inside of that to redirect as above (which works fine.)

Any help would be greatly appreciated!

Upvotes: 0

Views: 303

Answers (1)

Jonathan S.
Jonathan S.

Reputation: 2228

/(.*) will match a slash followed by zero or more characters, which means http://blog.example.com/ would match and be processed by this rule. Changing it to /(.+) would only match a slash followed by one or more characters, which should stop the root redirection. What about something like this:

Redirect 301 / http://www.example.com/blog
RedirectMatch 301 /(.+) http://www.example.com/blog/article/$1

This would work if put in a virtual host for blog.example.com, assuming [www.]example.com was in a separate virtual host. If you're handling them all from within a single vhost, this would redirect to itself endlessly. The only way I know of to handle it in one vhost is with mod_rewrite. Something like this, perhaps:

RewriteEngine On

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

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

Upvotes: 1

Related Questions