2

So I've been trying to get this redirect code to work for a few hours now and it's driving me crazy. I have the opposite direction (non-www to www) working with this code:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*) http://www.%{HTTP_HOST} [R=301]

I found the above code here: https://stackoverflow.com/questions/3013730/redirecting-non-www-to-www

I had to remove the $1 from the third line of code to get it to work:

RewriteRule ^(.*) http://www.%{HTTP_HOST}/$1 [R=301]

What I need is for www.example.com to redirect to example.com

and for every page on www.example.com to redirect to the exact same url, minus the www.

www.example.com/test ---> example.com/test

www.example.com/begin.php ---> example.com/begin.php

So on and so forth. Since I had the topmost code section working in the opposite direction, I thought it would be easy to just switch a couple of things around and have it working in a few minutes, but I always end up running into 404s, infinite loops, and what have you. The main problem is that I don't fully understand this part of the code:

^[^.]+\.[^.]+$

Any help for an apache greenie? Thanks a million!

---EDIT---

I found a code that is closer to what I want, but it has its own share of problems. The code is:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^.*$ http://example.com%{REQUEST_URI} [R=301,L]

There are two problems with this code:

1) When I type in example.com/test, it first passes through www.example.com/test, which I would like to avoid if at all possible.

2) When I type in example.com/test, it ends up at example.com/test.php, and I would like to avoid having the file with the .php extension as the final destination. Any suggestions?

Community
  • 1
  • 1

0 Answers0