Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I redirect a user using Apache Rewrite, to the fully qualified domain name?

I'm really new to apache mod_rewrite module. I have a page called http://abc in my company intranet. I want users to be redirected to http://abc.somecompanyname.com whenever they type http://abc to the URL bar. Could someone please provide and example or point me in the right direction.

I figure this should be quite an easy question to answer. Thanks everyone for you inputs.

-Mark

like image 357
Mark Avatar asked Jun 12 '10 09:06

Mark


3 Answers

Quote from Apache 2.4 documentation:

The very best way to solve this doesn't involve mod_rewrite at all, but rather uses the Redirect directive placed in a virtual host for the non-canonical hostname(s).

<VirtualHost *:80>
  ServerName undesired.example.com
  ServerAlias example.com notthis.example.com

  Redirect / http://www.example.com/
</VirtualHost>

<VirtualHost *:80>
  ServerName www.example.com
</VirtualHost>

This does require another virtual host, but there's no shortage of those. The solution works very well for me - and I like how redirection of 'unwanted' hosts and configuration of the canonical host are separated.

like image 152
ssc Avatar answered Oct 14 '22 00:10

ssc


You could accomplish that with a VirtualHost definition as simple as this, on the server handling requests for abc:

<VirtualHost *:80>
    ServerName abc
    RewriteEngine on
    RewriteRule ^/(.*)$ http://abc.somecompanyname.com/$1 [R,L]
</VirtualHost>
like image 5
Joseph Spiros Avatar answered Oct 13 '22 23:10

Joseph Spiros


I found the advise in the Apache2 URL Rewriting Guide worked better.

I ended up with:

RewriteEngine on
RewriteCond %{HTTP_HOST}   !^foo\.bar\.com [NC]
RewriteCond %{HTTP_HOST}   !^$ 
RewriteRule ^/(.*)         http://foo.bar.com/$1 [L,R]

The "RewriteEngine on" line wasn't included in the Apache2 example. Maybe it's usually on by default but in my case I needed to add it.

like image 2
George Hawkins Avatar answered Oct 14 '22 00:10

George Hawkins