Forum Moderators: phranque

Message Too Old, No Replies

301 redirect from non www to www.

How to do it in httpd.conf?

         

wfernley

8:30 pm on Mar 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was curious if anyone can help me redirect my non www to www. in the Apache httpd.conf file.

I have found a lot of posts discussing this but none show how to do it in the httpd.conf.

Can anyone help?

Thanks in advance for your time.

Wes

jdMorgan

10:35 pm on Mar 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The method is exactly the same. SImply add a leading slash to the RewriteRule pattern.

Jim

wfernley

2:50 pm on Mar 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the reply Jim.

I have added a code to my httpd.conf but it aint workin!

This is what I got.

<Directory />
Options FollowSymLinks
AllowOverride None
RewriteEngine On

# Check to see if requested page exists
RewriteCond %{REQUEST_fileNAME}!-s
RewriteCond %{request_filename}!-d
RewriteCond %{request_filename}/!-d

# Redirect non-www to www
# RewriteCond %{HTTP_HOST}!^mysite\.com [NC]
# RewriteRule (.*)http://www.mysite.com/$1 [R=301,L]

Do you have any ideas of why its not working?

jdMorgan

3:10 pm on Mar 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Because of syntax errors, probably. There is no rule (nothing to do) after your first set of RewriteConds -- I assume you want to rewrite requests for non-existent pages to a script, but the code does not say so.
The domain redirect code was incorrect and commented-out as well. And finally, the functions were in the wrong order.

# For use in httpd.conf or conf.d
Options +FollowSymLinks
AllowOverride none
RewriteEngine on
#
# Redirect all non-canonical domains to canonical www domain
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule ^/(.*) http://www.example.com/$1 [R=301,L]
#
# Check to see if requested page exists as a file or directory. If not, rewrite to script
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}/ !-d
RewriteRule ^/(.*) /some_script.php?requested_page=$1 [L]
#

If you get a server error, look at your server error log -- It will often tell you exactly what's wrong.
If "nothing happens", first make sure you've flushed your browser cache, then if that doesn't help, look into the config settings for options that disable mod_rewrite.

Jim