PDA

View Full Version : Using mod rewrite to force people to use "www" in the url


Brother Malachi
04-20-2009, 07:17 PM
I'm trying to force an auto redirect on those that don't use "www" in their url. Does anyone know how to accomplish this using .htaccess and mod rewrite?

The reason being that vb uses cookies to store login data. So if I goto http://www.myforum.com and login, then goto http://myforum.com I will still be logged out since the cookies stored were for the "www" domain. This becomes a huge issue with several mods that use hand written links and not dynamically generated. (I.e. instead of using the current address (meaning, it will use "www" in the url if the current url has it), they use a hand written one...regardless of whether the link has "www" in it or not. I.E. one of the mods inserted a link into my navbar that is always without a "www". Therefore, users often login with "www" in the url, then click on the link and are logged out.

Lynne
04-20-2009, 07:25 PM
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite\.com
RewriteRule (.*) http://www.yoursite.com/$1 [L,R=301]

Brother Malachi
04-20-2009, 07:32 PM
Super quick response! Thanks Lynne.

I already have few other rules in there, does it matter what order they go in?

Lynne
04-20-2009, 07:33 PM
I don't think so.

Brother Malachi
04-20-2009, 07:42 PM
Awesome, it worked!
:high five:

--------------- Added 1240261256 at 1240261256 ---------------

One more quick question. Our hosting provider gives us access via "/~name/" appended to the end of the url for some reason.
So when we go to www.mysite.com/~name/ the root directory loads and www.mysite.com/~name/forums/ loads the forum directory.

I tried writing a condition that would detect the "~name" and remove it from the url but apparently the server treats www.mysite.com/~name/ as www.mysite.com and .htaccess isn't able to detect the "~name" in it. (unless I wrote the condition wrong)

Any idea how this can be fixed?
The reason THIS is an issue, is because some of the users still use the url with "~name" in it since that is what they bookmarked.

kermit2
04-21-2009, 10:34 PM
Just as a side note, if you set the cookie domain to .mysite.com (with a dot at the beginning) via the ACP, your cookies will be good for mysite.com, www.mysite.com, whatever.mysite.com etc

RedSpiral
04-25-2009, 09:55 AM
I normally use this:

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

BSMedia
04-26-2009, 02:18 PM
Awesome, it worked!
:high five:

--------------- Added 1240261256 at 1240261256 ---------------

One more quick question. Our hosting provider gives us access via "/~name/" appended to the end of the url for some reason.
So when we go to www.mysite.com/~name/ the root directory loads and www.mysite.com/~name/forums/ loads the forum directory.

I tried writing a condition that would detect the "~name" and remove it from the url but apparently the server treats www.mysite.com/~name/ as www.mysite.com and .htaccess isn't able to detect the "~name" in it. (unless I wrote the condition wrong)

Any idea how this can be fixed?
The reason THIS is an issue, is because some of the users still use the url with "~name" in it since that is what they bookmarked.

Thats a server side setting and you'll have to tell your hosting company to either disable it or inform your users to update your bookmarks. the /~name is generally used when the domain is still propagating over to the new DNS, and shouldn't be used afterwards.

Carnage
04-29-2009, 04:30 PM
Is it a dedicated server?

As for the original query; A slightly better way than mod rewrite is to use your dns settings. If you set the @ record to redirect to www using a 301 redirect; It'll have the additional effect of consolidating search engine ranks over the two sub domains.

How you do this depends on your dns host

Ryan Ashbrook
05-01-2009, 12:33 AM
I use this method, you can try it but I can't garauntee it'll work without some adjustments.

Create a plugin and place it on the global_start hook with the following code:

if ( preg_match ( '#\/~name\/\/forums\/#ise', $_SERVER['REQUEST_URI'] ) )
{
$uri = str_replace ( '/~name/forums/', '', $_SERVER['REQUEST_URI'] );
header ( 'Location: http://www.mysite.com/forums/' . $uri, TRUE, 301 );
}

Remember to replace all instances of ~name and mysite.com with what they should be.