My question not focus on method of how base url is set in any framework of php. Rather what I actually want to know is what should be the base_url() From two options below
Suppose I select option 1 i.e http://example.com all the traffic to my website coming from http://www.example.com will face following problems, similarly if i select option 2 traffic coming from other otpion's url will face problem below.
Ajax blocked by browsers due to CORS(Cross Origin Request Sharing). Because google treats www and non www requests as from two different websites. My ajax code is below if that can be of any help
$.ajax({
url: '<?php echo base_url(); ?>/Index/perform_task',
type:'POST',
data: {
name: $("#name_field").val(),
task: $("#task_field").val(),
},
success:function(data){
if (data == 'task_completed'){
window.location.href = "<?php echo base_url() ?>";
}
else{
$('#task_error').html('Some Error occured.')
}
}
});
Are the benefits for any version
The short answer is no. There aren’t any advantages of www or non-www websites because it’s up to personal preference to choose one over another. The main aspect which affects SEO is your consistency, specifically whether you have been adding/removing www to your site’s URL. In order to benefit from this small thing, you need to choose one option and stick to that choice. In short, don’t change your site’s URL when you have already introduced it to well-known search engines.
Otherwise, they will treat both options (www.site.com and site.com) as two different websites and will ban them from doubling pages, keywords, and contents
The only difference between a www and non-www site URL is only a technical one. When your website contains “www.”, it is the hostname which adjusts to DNS and restricts cookies while utilizing other domains. Meanwhile, if your website doesn’t have “www.”, then it doesn’t have such a technical privilege
So what should you do ? Just use one of the version as you like and route all the requests coming to other version to the one you use, and never change it again.
As @junkfoodjunkie said, you should only use ONE domain and redirect the other. But if, for any reason, you have to keep the two domains, then you should call your ajax script within the current domain.
Then, your base_url() function should return the current domain. for example:
function base_url(){
return 'http'.(isset($_SERVER['HTTPS'])?'s':'').'://'.$_SERVER['SERVER_NAME'];
}