Forum Moderators: phranque
Editing httpd.conf, I have
- ensured "LoadModule rewrite_module modules/mod_rewrite.so" is uncommented
- Saved the httpd.conf file and restarted the Apache service after making all changes
- tried inserting the following code into the httpd.conf file
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
- have also tried according to another example
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.example.com/$1 [L,R]
Each time I try to go to http://example.com, the address never changes to http://www.example.com. I seem to be missing something crucial. Is there some simply way I can test this? Is the rewrite code supposed to be placed someplace specific in the httpd.conf file in order to work properly?
[edited by: jdMorgan at 7:42 pm (utc) on June 3, 2009]
[edit reason] example.com [/edit]
Second attempt: Add $ before [NC], Change R to R=301 too.
Optional: Change !^$ to be just . (a single dot). [Explanation: !^$ means 'not empty' and . means "is one or more characters'.]
Delete leading slash from pattern for use in .htaccess and include it for use in httpd.conf file.
Clear browser cache and try again.
I tried clearing the browser cache between each attempt. I am only using httpd.conf.
Tried this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} .
RewriteRule ^/(.*) http://www.example.com/$1 [L,R=301]
No luck.
Also tried:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
also, no luck.
and I tried:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule (.*) http://www.example.com/contact.html [R=301,L]
...just to verify if http://example.com would redirect to www.example.com/contact.html, but it does not.
Other suggestions? This section of code is sitting in the middle of my httpd.conf file - should it be relocated elsewhere?
[edited by: jdMorgan at 7:43 pm (utc) on June 3, 2009]
[edit reason] example.com [/edit]
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301,L]
Is your code inside the <VirtualHost> container for the appropriate domain? Is it also inside any <Directory> container? (The code changes slightly depending on whether it's within a <Directory> container, in that the path to that directory should be removed from the RewriteRule pattern -- In this case, remove the leading slash. If outside any <Directory> container, then the full URL-path from root should be specified.)
Jim
[edited by: jdMorgan at 7:41 pm (utc) on June 3, 2009]