Replies: 2 comments
-
|
Great question! This is a common challenge with GitHub Pages and relative asset paths. Here's how to handle this properly: Solution 1: Use the {% raw %} tag in Jekyll If you're using Jekyll (GitHub Pages default), add this to your HTML <base href="{{ site.baseurl }}/">This automatically adjusts all relative URLs based on your base path configuration. Solution 2: Update Your Jekyll Configuration In baseurl: "/june" # Your subpath
url: "https://basregx.github.io" # Your site URLThen reference assets with the baseurl filter: <link rel="stylesheet" href="{{ '/css/style.css' | relative_url }}">
<img src="{{ 'images/logo.png' | relative_url }}" alt="Logo">Solution 3: For Non-Jekyll Sites (HTML/CSS/JS)
<script>
window.basePath = '/june';
</script>
<!-- Instead of: -->
<link rel="stylesheet" href="/css/style.css">
<!-- Use: -->
<link rel="stylesheet" href="css/style.css"> <!-- relative path -->
const imageUrl = window.basePath + '/images/photo.jpg';Solution 4: Using the The simplest approach - add this in your <base href="/june/">This makes ALL relative paths resolve from Recommended Approach for Your Situtation: Since you're using absolute URLs starting with
Testing: Before deploying: # Test with baseurl
bundle exec jekyll serve --baseurl /june
# Visit: http://localhost:4000/june/Troubleshooting:
Let me know if you need help with any specific asset type (CSS, images, JavaScript)! |
Beta Was this translation helpful? Give feedback.
-
|
1. Problem Description When hosting a project site using GitHub Pages, the site is not served from the root of the domain. Instead, it is served under a repository subpath. Example project site: https://bsastregx.github.io/june/ In this configuration, the effective base path of the website becomes: /june/ However, many projects are written using root-absolute asset URLs such as: <script src="/js/app.js"></script>These paths begin with Therefore the browser attempts to load: https://bsastregx.github.io/css/style.css But the real files are located at: https://bsastregx.github.io/june/css/style.css Because of this mismatch, CSS, JavaScript, and image assets return 404 errors and the website may appear unstyled or partially broken. This behavior is common for GitHub Pages project sites. The example page where the issue can be observed is: https://bsastregx.github.io/june/#spacing # 2. Root Cause Analysis GitHub Pages supports two types of websites: User / Organization SiteFormat: Example: In this case the website is hosted at the domain root. Absolute URLs such as ### Project Site Format: https://username.github.io/repository-name/ Example: https://bsastregx.github.io/june/ Here the repository name ( Because of this, the actual base path becomes However, browsers do not automatically prepend this path when resolving root URLs. Therefore GitHub Pages does not automatically rewrite these URLs because it is a static hosting system and does not perform runtime HTML rewriting or request proxying. # 3. Why GitHub Pages Cannot Automatically Fix It GitHub Pages intentionally behaves as a simple static server. It does not: • rewrite absolute paths in HTML Therefore any asset URL that begins with instead of https://username.github.io/repository/ # 4. Correct Solution (Recommended) The most effective solution that preserves all existing asset URLs is to define a base path using the HTML The This allows existing asset URLs to work without rewriting every path in the repository. # 5. Implementation Steps Step 1 — Open the Main HTML FileOpen the primary entry file of the site. Typically: index.html ### Step 2 — Add a Base Path Declaration Inside the Example full head section: <title>Project Site</title> <script src="/js/app.js"></script>### Step 3 — Commit the Change Save the file and commit the change to the repository. Example workflow: git add index.html Step 4 — Wait for GitHub Pages DeploymentGitHub Pages will rebuild the site automatically after the push. Deployment normally takes between 30 seconds and 2 minutes. # 6. How the Fix Works Before the fix: /css/style.css is interpreted as: https://bsastregx.github.io/css/style.css After adding the base tag: the browser resolves the same path as: https://bsastregx.github.io/june/css/style.css No changes are required to the existing asset references. All CSS, JavaScript, images, and internal links will now resolve correctly relative to the project folder. # 7. Verification Procedure To verify that the fix is working correctly:
Confirm that asset requests load from paths such as: /june/css/style.css All requests should return HTTP status 200 instead of 404. # 8. Alternative Solutions If modifying HTML is not possible, the following alternatives may also be used. Option A — Convert Asset URLs to Relative PathsExample change: From: /css/style.css To: css/style.css ### Option B — Use a Static Site Generator Configuration If the project uses Jekyll, configure url: https://bsastregx.github.io/ Then reference assets using: {{ site.baseurl }}/css/style.css ### Option C — Use a Custom Domain If the site is configured with a custom domain such as: then the repository is served at the root path and absolute URLs will work automatically. # 9. Recommended Approach For the specific scenario described, the most practical and minimal change is: Add the following tag to the This approach: • requires only a single modification # 10. Final Result After implementing the base path configuration, the site: https://bsastregx.github.io/june/#spacing will load all assets correctly and function as intended without requiring repository-wide path modifications. |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Select Topic Area
Question
Body
I'm hosting a project site on GitHub Pages under a subpath (e.g., /june/). My HTML, CSS, and JS use absolute URLs starting with /, and I want them to resolve correctly without modifying the repository files. Is there a way to configure GitHub Pages so the base path points to the project folder, making absolute URLs work automatically?
https://bsastregx.github.io/june/#spacing
Many thanks in advance
Beta Was this translation helpful? Give feedback.
All reactions