Shakey
Shakey

Reputation: 89

301 Redirect Subdomain to subdirectory on an addon domain

My site setup is something like this

**Main Domain** - domain.com

**Addon domain** - addon.com

**blog sub domain** - blog.addon.com

**blog sub page** - blog.addon.com/post/54546/url-slug-here

Now addon.com is an add-on domain on a hostgator shared account where the root domain is domain.com

I want to make the blog.addon.com 301 redirect over to addon.com/directory/blog

For which I have tried

RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog.addon.com$ [NC]
RewriteRule ^/?$ "http://addon\.com/directory/blog/" [R=301,L]  

Unfortunately the above code does not work at all.

I also tried,

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

Both the codes above don't work. When I visit blog.addon.com - it simply shows me the default index.html I have up there.


Coming to Part 2 of my question,

I also need to redirect blog.addon.com/post/54546/url-slug-here over to addon.com/pagename and I have absolutely no clue how to achieve this.

I have looked up many queries and tried a bunch, but like the earlier I simply get a 404 and nothing beyond that.

I just want that particular URL redirected, not the whole set or wild cards.


I am thinking this has something to do with the domain being an addon over the main domain. But the wordpress permalinks code and my other local 301 redirects like domain.com/somepage to domain.com/someotherpage work perfectly.

Any help would be appreciated.

Upvotes: 1

Views: 4440

Answers (1)

Prix
Prix

Reputation: 19528

Given that your blog.addon.com is at the folder domain.com/blog, place the follow .htaccess inside the folder domain.com/blog:

Options +FollowSymLinks -MultiViews

RewriteEngine on
RewriteBase /

# Only redirect if the domain is:
RewriteCond %{HTTP_HOST} ^blog\.addon\.com$
# Redirect anything to the domain URL 
# http://www.addon.com/directory/blog/anything
RewriteRule ^/?(.*)$ http://www.addon.com/directory/blog/$1 [L,R=301]

However if you want to redirect to addon.com/blog change the RewriteRule to:

RewriteRule ^/?(.*)$ http://www.addon.com/blog/$1 [L,R=301]

And if you want to simple redirect it to the www.addon.com use it like this:

RewriteRule ^/?(.*)$ http://www.addon.com/$1 [L,R=301]

For your second part you can use this rule above the rule of part 1 on the .htaccess as rules are executed in order:

RewriteCond %{HTTP_HOST} ^blog\.addon\.com$
RewriteRule ^post/\d+/([^/]+)$ http://www.addon.com/$1 [L,R=301]

The above rule will redirect as a wildcard if u want the exact redirect you can use:

RewriteCond %{HTTP_HOST} ^blog\.addon\.com$
RewriteRule ^post/54546/url-slug-here$ http://www.addon.com/url-slug-here [L,R=301]

Upvotes: 3

Related Questions