Forum Moderators: phranque

Message Too Old, No Replies

www redirect in apache configuration

         

LinusIT

7:31 am on Sep 13, 2011 (gmt 0)

10+ Year Member



Hi

I've got a few sites running on my apache web server and would like to redirect non www to www at apache level, I.E instead of using .htaccess.

I have tried different methods but all seem to break apache and stop it from running.

Here's what I've got in my httpd-vhosts file:


<VirtualHost *:80>
ServerName domain.co.uk
ServerAlias *.domain.co.uk
#ServerAlias www.domain.co.uk domain.co.uk
DocumentRoot c:/www/webroot/test1
#Redirect 301 / [domain.co.uk...]
#RewriteEngine on
#RewriteCond %{HTTP_HOST} ^domain.co.uk
#RewriteRule (.*) [domain.co.uk$1...] [R=301,L]
</VirtualHost>


You'll see I've got several lines commented out, these are what I've tried but don't seem to work. At present the site is accessible using
http://domain.co.uk
and
http://www.domain.co.uk

lucy24

8:05 am on Sep 13, 2011 (gmt 0)

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



Did you leave out a slash / or did it get lost in the Forums' auto-url-conversion?

phranque

9:24 am on Sep 13, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



this should do it:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.co\.uk [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.example.co.uk/$1 [R=301,L]

LinusIT

1:00 pm on Sep 13, 2011 (gmt 0)

10+ Year Member



I've used the code you provided phranque and restarted apache but it gives me the following error message:

The requested operation has failed

If I comment those lines out apache starts with no problems.

Do I need to configure something else in order for it to work?

phranque

9:16 pm on Sep 13, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



did you check you server error log for clues?

if you can't find something interesting there, i would try an "apachectl configtest" which will check your apache config file syntax.

LinusIT

5:53 pm on Sep 14, 2011 (gmt 0)

10+ Year Member



I had to enable mod_rewrite for it to work, after I commented out the following line in httpd.conf it worked fine

LoadModule rewrite_module modules/mod_rewrite.so

g1smd

6:15 pm on Sep 14, 2011 (gmt 0)

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



Commented out?

That STOPS it working.

lucy24

9:30 pm on Sep 14, 2011 (gmt 0)

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



Fine-tooth-comb time. Are you inadvertently loading the same module twice? I assume, on no evidence whatsoever, that this would throw Apache into a tizzy-- mainly because everything else has that effect.

LinusIT

10:36 pm on Sep 14, 2011 (gmt 0)

10+ Year Member



Sorry!

After uncommenting it out, it works fine! Ooops