Forum Moderators: phranque

Message Too Old, No Replies

.htaccess non-www to www redirect problem?

         

blondynka

7:32 pm on May 28, 2010 (gmt 0)

10+ Year Member



Grr! Here is the code I put in my .htaccess file:

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


And yet, when I type in "http://example.com" I get redirected to "http://www.example.com/public/", which results in a 404 error (because the directory doesn't exist in that location).

Could my .htaccess file be in the wrong spot? Right now, it appears in the root directory of my ftp login (I still have to navigate through /public/ to get to my site files), which is where it was by default. Do I need to move it, or create a new one in a different directory? HELP!

Thanks :)

jdMorgan

9:50 pm on May 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you have any previously-executing rewriterules in this file... Or does your host use rules to implement some feature on your "account"?

It could be just a weird set-up. Since your rewriterule pattern will accept "anything or nothing," the "public" URL-path-part may already have been prepended to the URL-path by some other rule (or error). You could try modifying the rule to

RewriteRule ^(public/)?(.*)$ http://www.example.com/$2 [L,R=301]

instead -- just as a test. But you really need to find out how and/or why that "public" path-part is getting prepended, as this may cause you major headaches in the future -- bad redirects like this, scripts not working normally, etc.

Jim

blondynka

9:53 pm on May 28, 2010 (gmt 0)

10+ Year Member



Thanks for your suggestion jd, I will try it. The only other thing in the .htaccess file is this:

CaseInsensitive On
DirectoryIndex index.htm index.htm default.htm index.html default.html index.shtml default.shtml index.phtml default.phtml index.php default.php index.php3 default.php3 index.wml default.wml index.asp default.asp index.cfm default.cfm Default.aspx default.aspx default.asp Default.asp index.jsp default.jsp

I will try your suggestion and let you know what happens.

blondynka

9:58 pm on May 28, 2010 (gmt 0)

10+ Year Member



Eeeek... k I tried your code Jim, and while it worked for the http://example.com redirect, when I tried to access a page within the site just by clicking on the nav, Firefox gave me this:

"Firefox has detected that the server is redirecting the request for this address in a way that will never complete."

Don't think that one's gonna work :(

jdMorgan

10:04 pm on May 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I suspect that you will need to ask your host to correct your server configuration... /public should not and must not be publicly-detectable/accessible. If any push-back from them, then it's time to switch hosts, as this should never be happening...

"/public" is a typical path on commercially-hosted servers, but it must be "above the Web root" and should never be accessible via HTTP -- major, major security risk here...

If you do not use all of those types of directory index files, then pare the list back to those you do use, and put the most-likely filenames first. This will improve server performance.

Jim

blondynka

10:09 pm on May 28, 2010 (gmt 0)

10+ Year Member



Great thank you, I will look into that. Thanks for your help!

blondynka

10:40 pm on May 28, 2010 (gmt 0)

10+ Year Member



Just in case anyone else was following this thread, I contacting the hosting company, and it turns out the .htaccess file was in the wrong folder. Whoever had created the site before me had moved it. Works like a dream now. Phew!