Forum Moderators: phranque
I am trying create a .htaccess file in order to hide the html extensions in the URL and preferably add a trailing forward slash.
I have trawled the web looking for a direct example of this but have been unsuccessful so far, already having tried 10s of snippets of code that are supposed to work but don't in my situation. My regular expression know-how is negligible so I currently have no chance writing it from scratch.
Please has someone got an example I could use?
When I click on contact.html I would like to see the following URL: "http://www.mywebsitedomain.co.uk/test/contact/" instead of "http://www.mywebsitedomain.co.uk/test/contact.html" in the address bar.
Currently all my links have the ".html" extension e.g. "<a href="contact.html">contact us</a>"
Thank you very much.
Then that is step 3. Those links must be replaced.
Step 1 is to study-up on the documentation cited in our Apache Forum Charter.
Step 2 is to add some code to your .htaccess file to internally rewrite incoming client requests for "extensionless URLs" to the correct/appropriate files (which must have extensions) on your server.
A search for the quoted phrase in the preceding paragraph may prove helpful, but I strongly suggest that you don't skip step 1... Your .htaccess file is a server configuration file, and not to be treated lightly. If you copy and paste code "off some Web site", and that code has a tiny-but-fatal flaw in it, then how will you know that?
We find and discuss bad code sourced from major corporations here, and you're trusting anonymous authors of unknown ability with the health of *your* server? :o
Jim
Thanks anyway.
Yes the last thing I tried was:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}¦/)$
RewriteRule (.*)$ /$1/ [R=301,L]
but what happened was the trailing slash was added to my html file and the server would try to load a directory instead of the file and fail.
But even then, the *last RewriteCond of the second rule should stop that rule from being invoked if the first rule was already invoked.
So... The problem is likely elsewhere.
Do you have MultiViews enabled, or AcceptPathInfo turned on? If so,
Options -MultiViews AcceptPathInfo off AcceptPathInfo is only applicable to Apache 2.x servers.
The Options directive can be combined with any other pre-existing Options settings.
* File- and directory-exists checks are very expensive in CPU time as compared to local-variable evaluations. Therefore, the performance of your server would likely improve if you move the third RewriteCond in your second rule to that rule's first RewriteCond position and use the [NC] flag, and by using the RewriteRule pattern to eliminate requests for "/" from consideration by immediately rejecting blank URL-paths:
# Externally redirect to add a trailing slash if requested URL-path is not blank, does not
# end with a slash or a 'filetype', and does not resolve to an existing file or directory.
RewriteCond $1 !\.[a-z0-9]{1,5}$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*[^/])$ http://www.example.com/$1/ [R=301,L]
Also, there's an obscure +exploit that you can prevent by never allowing the client to control the beginning of the substitution path. Without detailing it here, I'd suggest that you change your first rule's substitution from "$1.html" to "/$1.html".
Jim
+ Cheers to member Caterham for pointing this out last year.