Forum Moderators: phranque

Message Too Old, No Replies

redirect to external url www and non www

mod_rewrite httpd .htaccess redirect

         

wyn_flown

4:36 pm on Apr 14, 2008 (gmt 0)

10+ Year Member



Hi,

I've inherited a web server and I have a slightly noobish question. I need to redirect a website I host to an external url. This is working fine with a .htaccess file in the sites directory. What I can't seem to get working is the original virtualhost non-www redirection.

Here's what I had originally in httpd.conf:

<VirtualHost *:80>
ServerName www.old-site.com
DocumentRoot /var/www/old-site
ErrorLog logs/old-site.com-error_log
TransferLog logs/old-site.com-access_log
</VirtualHost>

<VirtualHost *:80>
ServerName old-site
RewriteEngine on
RewriteRule ^/(.*)$ [old-site.com...]
</VirtualHost>

I added the following to enable my .htaccess file:

<Directory>
Options FollowSymLinks
AllowOverride All
</Directory>

My .htaccess file looks like this:

<Files ~ "^.(htaccess¦htpasswd)$">
deny from all
</Files>
Redirect permanent / [new-site...]
order deny,allow

All I need now is for requests to http: //old-site to be redirected to http: //www.new-site the same way that [old-site.com...] is.

Any help is appreciated!

g1smd

1:08 am on Apr 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Shouldn't "old-site" be "old-site.com"?

jdMorgan

1:20 pm on Apr 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The simplest solution I can suggest would be to use ServerAlias, and do it all at once in the first VirtualHost container:

<VirtualHost *:80>
ServerName www.old-site.com
[i]ServerAlias old-site.com[/i]
DocumentRoot /var/www/old-site
ErrorLog logs/old-site.com-error_log
TransferLog logs/old-site.com-access_log
Redirect 301 / http://www.new-site.com/
</VirtualHost>
#
[i]# Commented-out entire "old-site.com" vHost
#<VirtualHost *:80>
#ServerName old-site
#RewriteEngine on
#RewriteRule ^/(.*)$ http://www.old-site.com/$1
#</VirtualHost>[/i]

You won't need the .htaccess file any more.

Jim

wyn_flown

1:40 pm on Apr 17, 2008 (gmt 0)

10+ Year Member



I did mistakenly remove the .com in an edit and adding it back resolved the issue. I've since removed the .htaccess file altogether and gone with the ServerAlias method which is quite simple and clean compared to what I had thrown together.

Thank you!

g1smd

1:47 am on Apr 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Heh, jd; by posting that code snippet, you've also answered a question that I was about to ask in connection with something that I planned on doing tomorrow.

BradleyT

4:08 pm on May 8, 2008 (gmt 0)

10+ Year Member



I don't get why this isn't working

<VirtualHost *:80>
ServerName example.net
ServerAlias www.example.net example.net
Redirect 301 / http://www.example.edu/
</VirtualHost>

When I go into that VHost detail screen (using Webmin) it shows that a 301 redirect was created. However it still loads example.net instead of redirecting.

Oops - edited out IP.

[edited by: BradleyT at 4:09 pm (utc) on May 8, 2008]

jdMorgan

4:20 pm on May 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



example.net should not be included as a ServerAlias, since you've already declared it to be the canonical server name.

Did you completely-flush your browser cache before testing this redirect? If not, your browser will serve the page from its cache, and no request will be sent to your server, so your code can have no effect.

Jim

BradleyT

4:48 pm on May 8, 2008 (gmt 0)

10+ Year Member



I ended up calling our webhost and we didn't have a NameVirtualHost entry for the IP handing that request. He added that in and then it worked.