0

In my constants.php file, I have set site root.

 define("SITEROOT","http://www.example.com/");

please see for difference in http:// and http://www. in following description.

Now session_start works only if I move from http://www.example.com/index.php to any other page. But if I use http://example.com and tried to echo session details on other page with http://www.example.com/pagename.php, session is not continued.

Is there any way to auto correct url in browser's address bar to http://www.example.com if user uses http://example.com ?

darshan
  • 319
  • 3
  • 16
  • check this [link](http://stackoverflow.com/questions/12050590/redirect-non-www-to-www-in-htaccess) – InTry Dec 15 '15 at 20:44

3 Answers3

1

You can use your web server to force either www or non-www urls. It is highly recommended to use one of them (not allowing both) on live websites for search indexing perposes.

If you're using Apache you can do so by updating your site .htaccess file as follows for url's without www

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule .* http://%1%{REQUEST_URI} [R=301,L]

Update

As Vinny has pointed out in the comments it is recommended when ever possible to NOT use .htaccess and instead handle it in the Virtual Host Config file.

aharen
  • 627
  • 3
  • 13
  • @darshan Glad i could help, please remember that you not allow the website to be accessed with www and non-www when you get your website live :) – aharen Dec 15 '15 at 20:52
  • I didn't get your point in comment...Do u mean, only myexample.com with http and www in first position will work - open in browser and not only http in myexample.com ? because i tried ur code in htaccess and site without www prefixed is auto corrected with www prefixed in browser...which i want.. – darshan Dec 15 '15 at 20:56
  • @darshan I mean (for example) if your domain is mydomain.org don't allow the website to be visited via www.mydomain.org (with www) & mydomain.org (non-www) search engines such as google see this as duplicate contents and this effects your search ranking :) – aharen Dec 15 '15 at 21:01
  • 1
    This answer although correct is bad form for a production server, see http://stackoverflow.com/questions/1100343/apache-redirect-from-non-www-to-www – Vinny M Dec 15 '15 at 22:22
  • @VinnyMannello I agree with you here, VirtualHost must be used where ever possible but in most shared hostings sometimes .htaccess is the only option. I will update my answer to include that, Thank you for pointing it out :) – aharen Dec 15 '15 at 22:38
1

If you are using apache add this to your config.

<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / http://www.example.com/
</VirtualHost>
Vinny M
  • 762
  • 4
  • 14
  • ohhh..very good alternative... I will try this also and will let you know...thank you for suggestion.. – darshan Dec 15 '15 at 21:00
  • Rewrites are intense and messy: http://stackoverflow.com/questions/1100343/apache-redirect-from-non-www-to-www – Vinny M Dec 15 '15 at 22:06
1

You should be able to use something like this (before anything is displayed, or headers sent, so before your session creation code):

if( "www." != substr( $_SERVER["SERVER_NAME"], 0, 4 ) ){
    header( "Location: http://www.example.com".$_SERVER["REQUEST_URI"] );
    exit();
}

Alternately, you could create/modify your .htaccess file like this:

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

or maybe

RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^/(.*)$ http://www.%{HTTP_HOST}/$1 [R=301]
Jason Rush
  • 263
  • 1
  • 11
  • 1
    Thank you for your suggestions. It is perfect..But Ahren answered 6 minutes before you and I tried that solution first and it worked for me..so marking his answer as right / accepted answer.Sorry n Thank you once again – darshan Dec 15 '15 at 20:54