Automatic Re-Direct to correct URL ?

  • Thread starter Thread starter effdee
  • Start date Start date
E

effdee

Is it possible to have code on your index.html that detects whether
someone lands on an incorrect URL for your site

EG:
If someone typed in http://mysite.com instead of http://www.mysite.com
they would be automatically re-directed to the correct address.

Is this possible?

effdee
 
Yes, either server side (best option) using PHP, ASP, asp.NET etc -
whatever your server supports. Or using JavaScript to get the domain used,
compare to the correct domain and redirect if necessary. The caveat with
using JavaScript are:
1) the page loads completely, then reloads.
2) Some users have javascript disabled - this includes Search engines that
will list your site with the wrong domain.

Not tested: This goes into the <head> of the page.

<script type="text/javascript">
function chkDom() {
var dmn=document.domain
if dmn.indexOf("www.example.com") < 0 {
document.location.href = "http://www.example.com";
}}
window.onload = chkDom;
</script>
 
Ronx said:
Yes, either server side (best option) using PHP, ASP, asp.NET etc -
whatever your server supports. Or using JavaScript to get the domain used,
compare to the correct domain and redirect if necessary. The caveat with
using JavaScript are:
1) the page loads completely, then reloads.
2) Some users have javascript disabled - this includes Search engines that
will list your site with the wrong domain.

Not tested: This goes into the <head> of the page.

<script type="text/javascript">
function chkDom() {
var dmn=document.domain
if dmn.indexOf("www.example.com") < 0 {
document.location.href = "http://www.example.com";
}}
window.onload = chkDom;
</script>

I'll give that a try....
effdee
 
Ronx said:
Yes, either server side (best option) using PHP, ASP, asp.NET etc -
whatever your server supports. Or using JavaScript to get the domain used,
compare to the correct domain and redirect if necessary. The caveat with
using JavaScript are:
1) the page loads completely, then reloads.
2) Some users have javascript disabled - this includes Search engines that
will list your site with the wrong domain.

Not tested: This goes into the <head> of the page.

<script type="text/javascript">
function chkDom() {
var dmn=document.domain
if dmn.indexOf("www.example.com") < 0 {
document.location.href = "http://www.example.com";
}}
window.onload = chkDom;
</script>

Well I tried that code and it didn't work for me, in a standard HTML page.
Then i noticed you talk about PHP ASP asp.NET etc... I don't even know
what that stuff is!
I just have a normal HTML site, and need a bit of code to add to the
index.html

The other solution mentioned by someone else, was using a .htaccess
file, I can't do that on a free web site with no upgrades.

effdee
 
Try this:

<script type="text/javascript">

function chkDom() {
var dmn=document.domain
var pge = ""
if (document.location.href.length > (dmn.length + 6)) {
pge = document.location.href.slice(dmn.length + 7)
}
if (dmn.indexOf("www.") < 0) {
document.location.href = "http://www." + dmn + pge;
}}
window.onload = chkDom;
</script>

This has been tested, and needs no editing. It should work on almost any
domain[1], on any page.
[1] It will fail (gracefully) on a domain name such as examplewww.com -
note the location of the www.
 
Ronx said:
Try this:

<script type="text/javascript">

function chkDom() {
var dmn=document.domain
var pge = ""
if (document.location.href.length > (dmn.length + 6)) {
pge = document.location.href.slice(dmn.length + 7)
}
if (dmn.indexOf("www.") < 0) {
document.location.href = "http://www." + dmn + pge;
}}
window.onload = chkDom;
</script>

This has been tested, and needs no editing. It should work on almost any
domain[1], on any page.
[1] It will fail (gracefully) on a domain name such as examplewww.com -
note the location of the www.

Yes that works, in both FF and IE.
Thank you very much.
effdee
 
Back
Top