[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 412
  • Last Modified:

301 redirect of a subdomain in Apache

I'm redirecting a subdomain in .htaccess this way:

    RewriteCond %{HTTP_HOST}        ^subdomain\.domain\.com
    RewriteRule ^$                 http://www.domain.com/en/keyword       [R=301,L]

On the first look, this works fine, but not entirely.

http://subdomain.domain.com/ gets redirected to http://www.domain.com/en/keyword

http://subdomain.domain.com/en/keyword does not get redirected at all!

I need a solution that will redirects all URLs of a specific subdomain to a single page.

1 Solution
dMaAuthor Commented:
Increased points to 500.
Easiest is to make virtual host for sub-domain and add a single redirect there.
Steve BinkCommented:
Can you turn on your RewriteLog?  If so, set the level to 9 and post the log for a single browsing attempt to a URL that does not forward.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Michael WorshamInfrastructure / Solutions ArchitectCommented:
Since the information given is rather limited (more examples would be nice), perhaps something like this will work for you:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

Open in new window

Or, even shorter than mwecomputers' four config lines, you can create a subdomain virtual host in Apache, and then just include this one line:

Redirect 301 / http://server.com/subdirectory/
Steve BinkCommented:
In reference to mwecomputers' suggestion:

1) the [NC] flag may be important for you.  Make sure it is in your rule.
2) the rule highlights what I missed: the resource detection.  The difference being that you want all requests to go to a single page, so alter the rule as follows:

RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [NC]
RewriteRule .* http://www.domain.com/en/keyword [R=301,L]

Open in new window

As far the recommendation for a separate virtual host, it will work (indeed, my preferred method) if you have access to the server's config file.  Your question asked only about .htaccess, which means mod_rewrite is probably your best bet.
Your RewriteRule specifically states that it should only redirect where there is NOTHING after the HOST by specifying ^$:

RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com
RewriteRule ^$ http://www.domain.com/en/keyword [R=301,L]

I think what you WANTED is:

RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com
RewriteRule .* http://www.domain.com/en/keyword [R=301,L]

This will effectively redirect WITH or WITHOUT a path following the HOST.
dMaAuthor Commented:
Several correct answers, but mwecomputers was first.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now