Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 2
Message 1 of 4

GitHub Pages website connected to another domain, but I can still access via .github.io?

Solved! Go to Solution.

Hi,

I just configured the DNS for my domain (http://jackberingen.com) to redirect to my GitHub pages site. However, I am still able to access a broken version of the site by going to http://jackberingen.github.io/ which I do not want to be able to do. Any suggestions?

3 Replies
Copilot Lvl 2
Message 2 of 4

Re: GitHub Pages website connected to another domain, but I can still access via .github.io?

I have the same problem with my betting sites comparison website https://www.wettenerfahrungen.com. I'm not sure if it has something to do with the DNS configuration or with the programming code of my website. But I'm open for any solutions. Thanks!

Solution
Copilot Lvl 2
Message 3 of 4

Re: GitHub Pages website connected to another domain, but I can still access via .github.io?

I've fixed the problem, however, not how I would have liked to (without tampering with the actual website). Regardless, I've added a URL check which ensures the user is using the correct address within the index.js file of my React project. Even if you're not using React, you can still place this code anywhere as long as it runs when the page initializes. I've included a commented version of the code below, but again, you'll need to adapt this for your URLs and if you're not using React.

 

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './components/App';
import { BrowserRouter } from 'react-router-dom'
import registerServiceWorker from './registerServiceWorker';

//Get the window location. Even if it is at the base directory, it will still include a '/' at the end of the url.
let href = window.location.href;

//Get the base url without any forward-slashes (just in case the user was trying to navigate to other parts of the website via the .github.io url).
//E.G. https://jackberingen.github.io/portfolio-> https://jackberingen.github.io
let location = href.substring(0, href.indexOf('/', 8));

//Define a render function so we don't have to run the same code twice.
let render = ()=>{
  ReactDOM.render(<BrowserRouter><App /></BrowserRouter>, document.getElementById('root'));
  registerServiceWorker();
}

//Check if we're running locally. If we are, render the page.
if(location === "http://localhost:3000"){
  render();
}else{
  //If we're not running locally, check whether we're on the correct url, not the .github.io one.
  if(location !== "https://jackberingen.com"){
    //If we're not on the correct url, redirect the user including any other routes the user was trying to navigate to e.g. '/portfolio'.
    window.location.replace(`https://jackberingen.com${href.substring(href.indexOf('/', 8), href.length)}`);
  }else{
    //If we're not running locally but we're on the correct url, render the page.
    render();
  }
}

 

Hope this helps! :)

Community Manager
Message 4 of 4

Re: GitHub Pages website connected to another domain, but I can still access via .github.io?

Hey @jackberingen, Thanks for coming back to share what worked for you!

Thanks for being here, sharing, and keeping our community awesome!
Best,
AndreaG

Mark helpful posts with Accept as Solution to help other users locate important info. Don't forget to give Kudos for great content!