Mobile app version of vmapp.org
Login or Join
Barnes591

: Removing .html and index.html from URL I'm having some problems trying to Remove the .html extension from URLs Removing 'index.html' from an URL 1) To remove the extension I have tried using

@Barnes591

Posted in: #Htaccess #Url #UrlRewriting

I'm having some problems trying to


Remove the .html extension from URLs
Removing 'index.html' from an URL


1) To remove the extension I have tried using this in my htaccess file.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ .html


However when I click links in my HTML such as <a href="abcde.html"></a> it doesn't remove the .html from the URL and I am left with website.com/abcde.html
2) I tried using this to remove the index.html

RewriteCond %{THE_REQUEST} /index.(php|html) HTTP [NC]
RewriteRule (.*)index.(php|html)$ / [R=301,L]


But when I load an index.html file on my server, my URL looks something like this
website.com/folder//

I am left with an extra / at the end.

Can anyone help me out?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Barnes591

2 Comments

Sorted by latest first Latest Oldest Best

 

@Cody1181609

Building on the answer Anagio provided, let's cover all your requirements:


Transparently serve .html files on requests like /path/to/file if /path/to/file.html exists
Deny direct requests against /path/to/file.html
Serve /index.html on requests against /


Directory configuration:

Options +FollowSymLinks -MultiViews

DirectoryIndex index.html

RewriteEngine On
#
# Rewrite valid requests on .html files
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html?rw=1 [L,QSA]
#
# Return 404 on direct requests against .html files
RewriteCond %{REQUEST_URI} .html$
RewriteCond %{QUERY_STRING} !rw=1 [NC]
RewriteRule ^ - [R=404]

10% popularity Vote Up Vote Down


 

@Si4351233

This is from stackoverflow

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On

# To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme