Forum Moderators: phranque

Message Too Old, No Replies

Rewrite problems

         

vx2009

6:10 pm on Jun 3, 2009 (gmt 0)

10+ Year Member



I am having trouble getting rewrites to work. The first thing I am trying to do is very simply redirecting http://example.com to http://www.example.com, by trying to follow the examples I have seen all over the web. I've checked several solutions in previous posts but I am still not getting this to work. (I'm very new to this)

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]

g1smd

6:32 pm on Jun 3, 2009 (gmt 0)

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



First attempt: ^(.*)$ simplifies to (.*) here.

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.

vx2009

7:23 pm on Jun 3, 2009 (gmt 0)

10+ Year Member



Thank you for your response. I'll include the actual website this time.

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]

jdMorgan

7:40 pm on Jun 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Fixing several minor code and operational problems, I'd use:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301,L]

However, several of your attempts should have done *something* (if not exactly what you wanted), so this isn't likely to fix the main problem.

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]

vx2009

8:42 pm on Jun 3, 2009 (gmt 0)

10+ Year Member



Superb!

I included this last modified code in the VirtualHost container and everything works brilliantly and exactly as I need. I included modified versions of this code in my other VirtualHost containers too. Question answered!

Thanks for your help to both of you.