Forum Moderators: phranque

Message Too Old, No Replies

Redirect to www and keep URL parameters

         

superwad

3:36 am on Jan 12, 2009 (gmt 0)

10+ Year Member



Sorry if this has been asked and answered before, but I'm at my wits end with this problem.

I'm trying to keep all access to any file on my website to go through the www subdomain. All my URLs are already being generated using the www, but it doesn't prevent people from accessing the site without the www.

I've tried to solve this in the past, and always get frustrated and give up. I'm currently using this code:

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

The problem with this is very annoying. If I go to a URL such as example.com/folder/file.ext, I get redirected to www.example.com/file.ext.

If I go to example.com/folder, I get redirected to www.example.com. If I go to example.com/folder/, I get redirected properly.

Is there a way out there, to simply add the www. to the beginning of the URL, and keep the full path, with all parameters intact? This site uses many levels and is dynamically created, and I can't have users being redirected to the root when the server attempts to fix an access problem on their end.

Much thanks for any help!

jdMorgan

6:36 am on Jan 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code you posted is sufficiently correct, and won't cause the problem you describe by itself.

You've likely got some other mod_rewrite rules or mod_alias directives that are interfering -- in this .htaccess file or in subdirectory .htaccess files, or in server config-level files such as httpd.conf or conf.d, etc.

This problem could also be caused by mod_negotiation (Options +MultiViews) or by 'AcceptPathInfo on'.

But you may not be able to just disable the other function(s) to stop the interference -- Your complex site may require those features.

Jim

superwad

7:45 am on Jan 12, 2009 (gmt 0)

10+ Year Member



Well, the .htaccess was placed as a top-level file, and there are no other .htaccess in any of the paths I'm using. The only contents of the file are what I posted.

As for the server-level configuration, I can't verify that myself. This site is hosted on a shared server. Is there any way that I can confirm other settings, and possibly override them?

If I can't, what should I ask my hosting company to do in order to enable the functionality I've described?

Thank you for your reply.

jdMorgan

2:18 pm on Jan 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you using MultiViews for content-negotiation? If not, then

Options -MultiViews

can be used to disable that feature.

This can be combined with your existing +FollowSymLinks Option, as in


Options +FollowSymLinks -MultiViews

Similarly, AcceptPathInfo can be disabled using

AcceptPathInfo off

If neither of these help, or if your site depends on either of these features to function and changing these settings breaks your site, then it's time to ask your host what is going on with your server, as it is likely that some other module is acting on the request before mod_rewrite 'sees' it. So, in order to form a plan to fix the problem, you'll need to know the details of your server configuration.

Jim