Forum Moderators: phranque

Message Too Old, No Replies

http.conf problem

Can't get permanent redirect to work

         

Zig28

1:34 am on Feb 8, 2005 (gmt 0)

10+ Year Member



Hi everyone:
My first post, I hope I'm in the correct forum. I've looked through many posts but can't find the solution to my problem, and hoping someone can help.

I'm trying to use http.conf to do a permanent redirect from mysite.com to www.mysite.com. I can't use mod_rewrite in .htaccess because FrontPage chokes on it.

I have my own server with several websites, and in looking through the existing http.conf file, it shows a virtual host for the site, so I figured all I had to do was add this line in the virtual host section for the site in question:
PermanentRedirect / [mysite.com...]

But when I do that and restart Apache, all the other sites come back ok but mysite.com won't load. As soon as I take out the PermanentRedirect, it works. Is there something I'm missing? Thanks..

Here is the existing section in http.conf, the PermanentRedirect I tried was added after the "ServerAlias" line:

<VirtualHost 111.11.111.11>
ServerAdmin webmaster@mysite.com
DocumentRoot /home/mysite/public_html
BytesLog domlogs/mysite.com-bytes_log
ServerName www.mysite.com
User xyz
Group xyz
ServerAlias mysite.com www.mysite.com
CustomLog domlogs/mysite.com combined
ScriptAlias /cgi-bin/ /home/xyz/public_html/cgi-bin/
</VirtualHost>

jdMorgan

2:26 am on Feb 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Zig28,

Welcome to WebmasterWorld!

Yes, your code will redirect both mysite.com and www.mysite.com to www.mysite.com -- You've created a redirection loop. The client will be redirected until it (or the server) reaches its maximum redirection limit. You should see this error clearly stated in your server error log.

To solve this problem, you'll need to use mod_rewrite within a container (such as your existing <VirtualHost>) in httpd.conf.


RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain\.com
RewriteRule (.*) http://www.mydomain.com/$1 [R=301,L]

This does the redirect only if the requested hostname is "mydomain.com" (with any optional port number appended). See the links in our forum Charter for more info on mod_rewrite and regular expressions.

Jim

Zig28

2:50 am on Feb 8, 2005 (gmt 0)

10+ Year Member



Thanks Jim, I'll try using this within the virtual host, but not sure if FrontPage will like it. I think I'll wait until tomorrow though, as I've done enough Apache reboots for one night and don't want to scare off anyone!

Zig28

11:40 am on Feb 8, 2005 (gmt 0)

10+ Year Member



As I suspected, FrontPage doesn't like the mod_rewrite solution, so I'd really like to use the http.conf solution if possible.

I don't understand why the http.conf solution I tried doesn't work - it works perfectly for a friend of mine. Below is the code from http.conf that works on her site. When I try it, it doesn't work, the site in question will not load.

The way I understand it, the first VirtualHost statement sets up a virtual site called mysite.com and then redirects everything to www.mysite.com. The second virtual host has always been in the http.conf by default. Any hints on why this works on one server but not another?

<VirtualHost 11.11.111.11>
BytesLog domlogs/mysite.com-bytes_log
ServerName mysite.com
Redirect permanent / [mysite.com...] </VirtualHost>

<VirtualHost 11.11.111.11>
ServerAdmin root@mysite.com
DocumentRoot /home/mysite/public_html
BytesLog domlogs/mysite.com-bytes_log
ServerName www.mysite.com
User mysite
Group mysite
ServerAlias mysite.com www.mysite.com
CustomLog domlogs/mysite.com combined
ScriptAlias /cgi-bin/ /home/mysite/public_html/cgi-bin/
</VirtualHost>

Zig28

1:03 am on Feb 9, 2005 (gmt 0)

10+ Year Member



Just wanted to follow up for the record, I did finally fix this using Jim's advice, which I had got wrong the first time.

I added this redirect (below) to the VirtualHost for my website that already exisited in my httpd.conf file. The redirect from "mysite.com" to "www.mysite.com" now works AND FrontPage has no problems with it:

<VirtualHost 111.11.111.11>
ServerAdmin me@mysite.com
DocumentRoot /home/mysite/public_html
BytesLog domlogs/mysite.com-bytes_log
ServerName www.mysite.com
User meandmy
Group meandmy
ServerAlias mysite.com www.mysite.com
CustomLog domlogs/mysite.com combined
ScriptAlias /cgi-bin/ /home/meandmy/public_html/cgi-bin/
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite\.com
RewriteRule ^/(.*) [mysite.com...] [R=301,L]
</VirtualHost>

Note that the ^/ before the (.*) eliminates the extra slash character, found this tip in another webmasterworld thread.

I tested this using one of the redirect test websites that checks to make sure the redirect works and that it is "search engine friendly" and it passed.

Thanks for the help and I hope this information will help someone else!