I would like to know if there is a way to edit the .htaccess file because in Search Console it shows 404 errors, and I want to redirect them to 410. Where can I edit this part?
Thank you.
Could you please provide some more details?
URL, hosting provider, error, etc
Also, it’s unlikely that htaccess is being used given Ghost tech stack
If you create a error-404.hbs
file in your theme root directory, you can add the Search form in this template. Now all 404’s get a page with a search box.
But since you want a 410 response on the 404, embed this js on error-404.hbs
<script>
if (window.history && window.history.replaceState) {
// Replace the current state with a 410 status
window.history.replaceState(
null,
'',
window.location.href
);
// Override the status code in the browser
Object.defineProperty(document.querySelector('html'), 'httpStatusCode', {
get: function() { return 410; }
});
}
</script>
Hi, mheland, thanks for the information. I have a question: how could I add this to Ghost? Do I need to install a plugin or make any adjustments? And if the pages with error 404 no longer exist, how could I include the code in those cases?
Thanks,
Regards
If a page no longer exists, Ghost will send your visitors to error-404.hbs
page, so that will take care of it.
- Download the Ghost theme file ZIP
- Unzip to new directory
- Create a text file named error-404.hbs in the root of the folder
- Insert the code below and save
- Create a new ZIP of the folder
- Upload to Ghost themes and activate
Use this HTML to create a page error-404.hbs
in the root of your theme directory. A 404 page not found will now respond with 410 instead.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>410 Gone</title>
<style>
body {
font-family: system-ui, -apple-system, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f5f5f5;
}
.error-container {
text-align: center;
padding: 2rem;
background: white;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
max-width: 500px;
}
h1 {
font-size: 3rem;
color: #333;
margin-bottom: 1rem;
}
p {
color: #666;
margin: 1rem 0;
}
</style>
<script>
if (window.history && window.history.replaceState) {
window.history.replaceState(null, '', window.location.href);
Object.defineProperty(document.querySelector('html'), 'httpStatusCode', {
get: function() {
return 410;
}
});
}
</script>
</head>
<body>
<div class="error-container">
<h1>410</h1>
<p>This resource is no longer available and will not be available again.</p>
</div>
</body>
</html>