I am designing a web page and want to display a code snippet if the website is viewed without www like http://example.com
but not when viewed with http://www.example.com
.
<script type="text/javascript">
if (window.location.href== "http://www.example.com") {
window.location.href = 'http://example.com/';
}
</script>
Using this I can redirect users from www version to non www version.
but it would be better if there is a way if the code which I want to add only display when address in the address bar is http://example.com
and display something else when url is http://www.example.com
.
I think this could be done using similar javascript code.
But using a document.write .