Ask coding questions

← Back to all posts
Is there a way to remove the ".html" extension with .htaccess using repl.it?
GavHern (65)

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 (535) [earned 5 cycles]
View Answer
Commentshotnewtop
a5rocks (535)

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)

GavHern (65)

@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

a5rocks (535)

@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