开发者

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

开发者 https://www.devze.com 2023-01-03 00:59 出处:网络
I\'m really new to apache mod_rewrite module.I have a page called http://abc in m开发者_Python百科y company intranet.I want users to be redirected to http://abc.somecompanyname.com whenever they type

I'm really new to apache mod_rewrite module. I have a page called http://abc in m开发者_Python百科y 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


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.


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>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号