I have declared the base url as $config['base_url'] = 'http://example.com/';.
But the issue is if someone try to access my website with 'http://www.example.com/'; , css/ js functions are not working.
If I change the base url to 'http://www.example.com/' , users cannot access with 'http://example.com/'.
I am using codeigniter 3.0.3 . This is an issue with a website which is already online.
CSS - assets/plugins/bootstrap/css/bootstrap.min.css" />
JS - ">
. btw , ajax request are also failed.When I try to access an ajax function the bellow error showing on console –
XMLHttpRequest cannot load http://www.example.com/Dynamic/home/normal/4. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.com' is therefore not allowed access.
css error
Font from origin 'http://www.example.com' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.com' is therefore not allowed access.
Change your base url from
$config['base_url'] = 'http://example.com/';to
$config['base_url'] = 'http://www.example.com/';
Reason:www and non-www website url are considered as different website so it gives the error of
Better way is the add code in htaccess to convert non-www to www.
Hope this explain the reason of change...
Define baseUrl like this
and
.htaccess
should beI think the following code-block shows the best way to configure the base_url in Codeigniter :
And I strongly believe that this will solve your problem. Thank you.