views:

53

answers:

3

for some weird reason my CMS is logging out if the address bar does not have www before the full website name. for example, when we enter xyz.com, it takes me to the website but then it wont show as logged in and if i type in www.xyz.com it will find the cookie and show me logged in.

What i want to do is, when user types in xyz.com, i want it to directly (transparent to user) go to www.xyz.com. I want to add that www before xyz.com. I tried adding a .htaccess file in the directory where index.php is present and this is code in htaccess file.

DirectoryIndex index.php
Redirect xyz.com www.xyz.com/index.php

The .htaccess file is disappearing when i transfer it over ftp filezilla.

A: 

try this in the htaccess file:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^xyz.com$
RewriteRule ^/?$ "http\:\/\/www.xyz.com" [R=301,L]

However, your problem sounds like cookie related. Probably the CMS is using a cookie to check the log in status, but the cookie domain param is 'www.xyz.com' instead of '.xyz.com'.

--- edit ---

improved a bit the final line of the code (it is tested and working), but as tcp said, mod_rewrite must be enabled. If you can't enable it, try the code that Lobsterm posted and if you can't do this either, you could try to change the cookie domain param from 'www.xyz.com' to '.xyz.com'

aletzo
yes, the cookie is present but is not being used when xyz.com is present in the url. Only when www.xyz is present then the cookie is being used.
Scorpion King
Ok i have tried to place `Options +FollowSymlinksRewriteEngine onrewritecond %{http_host} ^xyz.com [nc]rewriterule ^(.*)$ http://www.xyz.com/$1 [r=301,nc]` in my .htaccess file and i have placed this file at the same palce where index.php is present but it is not working ? :(
Scorpion King
A: 

If you are willing to modify your index.php you could add the following logic to the top of the file:

/*This is a tempory redirection from mysite.com to www.mysite.com*/
if($_SERVER['SERVER_NAME'] == 'mysite.com')
{
    $redirect =     $_SERVER['REQUEST_URI'];
    header( 'Location: http://www.mysite.com'. $redirect ) ;
}
Lobsterm
Wow. thanks. that solved my problem in a second :)I do not have to deal with htaccess or anything now. Good job Lobsterm :)
Scorpion King
A: 

If you want to use rewrites, make sure mod_rewrite is being loaded in your Apache conf file and check that the AllowOverride parameter is either set to All or the just the directives you want to be allowed in .htaccess

Also as aletzo said, you probably want your cookie to cover your whole domain so change the cookie domain from www.example.com to example.com . Then, it won't matter if user are accessing with a www prefix or within a subdomain.

EDIT: Glad you found the answer you were looking for, but if you need to make filezilla show you .htaccess in the future, Server -> Force showing hidden files

tcp
Thanks for the tip tcp :)
Scorpion King