Ask coding questions

← Back to all posts
How do I Get Rid of the .html extension?
JordanDixon1 (299)

How do I do this on repl.it? I have tried adding a .htaccess file but it doesn't work. Any way to do this?

Answered by mat1 (3239) [earned 5 cycles]
View Answer
Commentshotnewtop
mat1 (3239)

You can create a folder with the name of the path you want (ex. myhtml) and then insert a file called index.html inside it (it must have this exact name) with all the html you want. Then when you go to ...repl.co/myhtml it should show the html you put in index.html.

mat1 (3239)

@JordanDixon1 Oh, the issue is that you didn't name the file inside the folder "index.html". (It uses the folder name for the url if you do this)

JordanDixon1 (299)

@mat1 Ohhhh. I had to rename the file index.html instead of page2.html thanks.

MrEconomical (2129)

if you mean in the url, you can set up a Node.js http server and handle requests by writing html files to specific urls. for example, if the request has url link/home, you can write the file home.html to it

JordanDixon1 (299)

@MrEconomical How would I go about doing this for every .html page in the coding language html,css,js?

MrEconomical (2129)

@JordanDixon1 ideally, you would have a finite amount of pages you want in your website. you can manually check each url and then give the corresponding html page.

Vandesm14 (2219)

@MrEconomical He could use middleware maybe?

Vandesm14 (2219)

@JordanDixon1 Like this

app.use((req, res) => {
    res.redirect(req.path.replace('.html', ''));
});

It checks every route instead of a single one.

JordanDixon1 (299)

@Vandesm14 How would I implement this in html,css,js?

JordanDixon1 (299)

@Vandesm14 Do I have to create a whole new nodejs repl?

JordanDixon1 (299)

@Vandesm14 How would I implement the html part?