问题描述:

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

网友答案:

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>
网友答案:

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.

网友答案:

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.

相关阅读:
Top