Skip to content
← Back to Community
Is there a way to remove the ".html" extension with .htaccess using repl.it?
Profile icon
GavHern

I tried updating my site to use an .htaccess file to remove the .html file extension from the URL. This did not work and I'm just getting the replit 404 pages. Here's my code:

RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.html -f RewriteRule ^(.*)$ $1.html [NC,L]

Is there an issue with my code or does repl.it not support .htaccess rules? If it's repl.it not allowing it, is there any way I can do this without an .htaccess just as easily? Thanks. (I'm also wondering, if I link my repl to an actual domain will this issue still occur?)

Answered by a5rocks [earned 5 cycles]
View Answer
Voters
Profile icon
Nathanielx3rd
Profile icon
ManuTheCoder
Profile icon
a5rocks
Profile icon
JustARatherRidi
Profile icon
GavHern
Comments
hotnewtop
Profile icon
a5rocks

I mean, I would just code a simple backend. Flask or express or whatever.

But yeah, repl should add .htaccess working. (idk if your code is wrong btw)

Profile icon
GavHern

@a5rocks I've tried my code on GitHub pages and it works fine. Any info on where I could learn to use flask or Express for this

Profile icon
a5rocks

@GavHern Well, I would just do something simple with Flask such as

from flask import Flask, send_from_directory app = Flask(__name__) @app.route('/') def send_index(): return send_from_directory('website', 'index.html') @app.route('/<path:path>') def send_code(path): return send_from_directory('website', path+'.html') @app.route('/css/<path:path>') def send_css(path): return send_from_directory('css', path+'.css') @app.route('/js/<path:path>') def send_js(path): return send_from_directory('js', path+'.js') app.run(host='0.0.0.0', port=8080)

Relevant SO question: https://stackoverflow.com/questions/20646822/how-to-serve-static-files-in-flask

If you are using the code above, put all your html files in a "website" directory, all your css in a "css" directory, and all your js in a "js" directory

The only thing is that Flask is dynamic, and so your site will be slightly slower. If you really want to counteract that, I would look into https://repl.it/languages/polygott, installing nginx using install-pkg nginx or whatever, and then customizing the command to allow .htaccess. If you do that, and post it on https://repl.it/feedback, I'm pretty sure repl will update html/css/js repls to allow .htaccess

Profile icon
orion0

@GavHern I might be a little too late but for any new users: Replit doesn't show a 404 page with that .htaccess code is bcs it doesn't give a 404 error this is how .htaccess code is supposed to work:

If the server gives a 404 error the .htaccess will tell the browser that this is the 404 page, but replit dosent give a 404 error but instead replit servers show their own 404 page so if the htaccess dosent get a 404 error it will think that page exists so it wont redirect to our 404 page

i hope i was able to explain you correctly, you can blame replit for this, but you can easily create a 404 page with js,please refer to this(https://replit.com/@Coder100/Easy-404-error-page-ayayayayayaya-pogger-sub?c=135352)

Profile icon
smuppana060

This question is old but for any future users looking to do this - You do not have to use .htaccess files. You can just create a folder with your desired name and place into it an index.html

Profile icon
bzzt

If there are still people who are wondering how to achieve this, it now is possible to use an htaccess file with a static HTML,CSS,JS project. Here's the code I used to redirect all .html links to static endpoints:

RewriteCond %{THE_REQUEST} \.html RewriteRule ^(.*)\.html$ /$1 [R=301,L]
Profile icon
nifhtnep71

@bzzt Is not working for me, :(
Can you show us an example?

Profile icon
orion0

@nifhtnep71 create a .htaccess file copy paste that code in that file then remove .html extensions from all the files and it should work, if it still doesn't work feel free to ask me again :) hope I am not too late