1

On a shared hosting environment based on CentOS-Bash, Apache (with Nginx as a "reverse proxy"), PHP and MySQL I host a MediaWiki website. Normally, no webpage has a .html extension.

I have created a CMS-agnostic contact form and placed its .html file in my website's root directory. I can access the form by the pattern example.com/contact.html and send emails to myself through it.

I might expand the contact form program by adding files such as success.html and in any case I might add other .html CMS-agnostic webpages in the future so in the long term I want to remove all .html extensions in URLs.


how to remove all .html extensions from all webpages of a website?
How could I ensure that no .html file appears in a URL bar with any .html extension file?
Perhaps there is some "brutal" APCRE (Apache Perl Compatible Regular Expression) directive solution for that? (A wildcard redirection?)

0
1

There is no single answer to this and any way you attack it will likely cause significant pain during the transition. I would focus on the problem you are issue you are trying to solve, rather then ask for how to implement a specific solution.

Some possibilities -

  1. Use rewrite rules to add .html onto the end of any url received [that doesn't end in .xx .xxx or .xxxx - You need this caveat so images, JavaScript, css etc will still work.

  2. Use rewrite rules to check if the given filename exists, and if not rewrite it with .html

Both of the above suffer limitatiins - how do you habdle cgi / php / non static content? Also - a biggie - how do you handle embedded links - this leads to -

  1. Put a reverse proxy in front of your web site/server which rewrites all links as they pass through. This will create an abstraction/compatibility layer with existing pages but will likely rewuire tweeking and cause unneccessary complexity - also frustrating upgrades moving forward.

A better way -

Many CMS's do it differently and better by creating a framework and parsing any URLs which don't match through a dynamic page which then parses the received URL, pulls the appropriate resources and displays them. By putting all the pages under programmatic control you can manipulate the URL as you see fit, changing yhe included/built pages/resources as appropriate. Also, because if the common support for WordPress most hosts support this out the box with a few standardised rewrite rules like

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.php [L]
</IfModule>

This checks that the given filename does not match an actual file or directory on the server and pushes it to index.php which is then free to check the URL and build a page. This is how WordPress can allow different formats for "pretty URLs" - and the same code xan be used to build a backward compatible "shim" between your static files and new code - even parsing existing content to remove .html extensions and acting almost like a reverse proxy.

1
  • Hi, I really have no intention to focus on a solution rather than on a problem here :) My only problem is that .html extensions appear and I don't want them to appear anywhere... I am sorry that I didn't declare that my hosting is shared hosting so my control of the web servers is somewhat limited. I have edited to declare that. – timesharer Mar 23 at 18:37
1

There are three parts to this:

  1. Making URLs without an extension work by loading the correct content
  2. Redirecting URLs ending in .html to remove the extension
  3. Changing all the links in your site so that they don't have the .html

The first part is built into the Apache content negotiation module. It has a feature called "Multiviews." That feature will take a URL path without an extension and serve a file with an extension. If there is only one matching file, it will serve that, but it has logic to deal with returning different content-types to different browsers or different languages to different users. To enable multiviews you just need to enable it as an option in your .htaccess file or the directory section of your Apache conf file:

Options +MultiViews

Alternately, you can use rewrite rules to implement the functionality only for .html files (source):

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule (.+)/$ $1.html [L]

For the second part, you need to redirect away from a URL that has a .html extension. You can implement that with rewrite rules:

RewriteCond %{THE_REQUEST} \.html\s
RewriteRule (.+)\.html$ /$1/ [R=301,L]

For the third part, I don't know of a way to configure it for a general case. Preventing your site from linking to the .html extension is going to be different in every case. For static files, you would need to edit them all and remove the .html from all links. Content management systems may or may not have settings to automatically remove the extension from all links.

4
  • Hi, thanks; I think that MediaWiki cannot control files which are not under its control (i.e. static HTML files). What is Changing all the links in your site so that they don't have the .html? I mean, if I remove the .html than as you know, the link would break. – timesharer Mar 23 at 19:44
  • Once you do the first two steps, you would have to edit every page in MediaWiki that had a link to your static HTML files and change the links not to have the .html extension. – Stephen Ostermiller Mar 23 at 20:06
  • I misunderstand --- all webpages in MediaWiki link to it via the navigation menu (there shouldn't be any other link) so the change should generally be just in one place. – timesharer Mar 24 at 3:39
  • Good that makes it easy. – Stephen Ostermiller Mar 24 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.