Forum Moderators: phranque
I did search and found a similar thread, however there were numerous responses offering different solutions (and some looking quite complicated!) First off, I'm a bit of a n00b in this area but I'll hopefully survive - heh. I currently have a website, and I want to remove the ".html" extension after *all* URLs. ie www.mywebsite.com/random instead of www.mywebsite.com/random.html
I understand that some lines have to be written in an .htaccess file. I know how to do this - but does this file simply need to be placed in the root directory (where all my pages are)? What is the necessary code to do this?
Thanks in advance!
Kyle Flanigan
More information on how to get the most from this forum is available in our Forum Charter -- Please see the link at the top of this page.
You may place .htaccess files in any HTTP-accessible directory. Depending on the setting of RewriteOptions inherit, subdirectories inherit the rules and policies of rewritrules in parent-directory .htaccess files, or they stand alone. The simplest --if not most-processor-efficient-- implementation is a single, centralized .htaccess file in your site root (home page) directory. Reduced processing efficiency is traded off with improved ease of centralized maintenance, according to your preferences.
Jim
As I understand it, if I remove all ".html" parts of the necessary links and add the following to my .htaccess file - this would solve the issue? (albeit with a slight performance issue)
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
rewriterule ^(([^/]+/)*[^./]+)/?$ /$1.html [L]
If you do that, then you can eliminate the two RewriteConds --and their associated performance issue-- if you're willing to say, "Rewrite any and all requests which do not contain a period or slash in the final URL-path-part to add a .html extension."
If your site's URL-architecture supports this convention now, then I'd recommend it because it's more efficient. And there are no "standard or customary" extensionless pages -- /robots.txt, /sitemap.xml, /labels.rdf, and /w3c/p3p.xml all have extensions, as would image and media files.
Jim
Please do a bit of testing and experimentation. It will save you time waiting on replies here, and you will gain a much better understanding of the issues.
Jim
Edit: I've done a bit of testing regarding the .htaccess file and file URLs, and the issue is at least 'semi-resolved'. I have routed navigation to simply go to <a href="links"> and the links page *does* work - however the URL still has the .html extension in it? I appreciate your help ..
Besides others, I tried the following two options -- to no avail.
First option:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) /$1.html [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP
RewriteRule ^([^.]+)\.html$ http://www.domain.com/$1 [R=301,L]
Second option:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule ^(([^/]+/)*[^./]+)$ /$1.html [L] In both cases, the redirection *only* works if the old file (page-name.html) still exists in the root directory of the site. However, when that file is removed from the root directory (whose name is now page-name), whenever I enter www.domain.com/page-name.html into the browser's addressbar to check if the redirection works, it gives me a 404 error.
Again, the host, the domain name, and all file names are the same; they're all just without the trailing .html now.
Apache version 2.2.9
I'm obviously missing a piece here.
Thank you very much in advance for your help.
~Marcus
That rewrite updates the pointers inside the server to the new filepath - and then you invoke a redirect which will expose that "secret" pointer out to the outside world.
Place the redirect to be before the rewrite, and the problem will likely go away.
I did what you said, and it still gives a 404 error when the static HTML file doesn't exist.
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP
RewriteRule ^([^.]+)\.html$ http://www.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) /$1.html [L]
~M.
the server logs should tell you which resource is eventually being requested and that should provide a hint about solving your problem.
Either way, I found a resolving .htaccess configuration in the meanwhile and am going to post it below for others to learn from, too.
With the following simple configuration, I can remove the old static HTML files while requests to those old files are being redirected to the respective new locations which are, again, without the .html file extension now:
RewriteEngine On
RewriteRule (.*)\.html $1 [R=301,L]
Anyway, browsing through your forum threads and learning from your posts was a big help, for which I'm very grateful.
Thank you.
~Marcus
Jim