: How do I implement URL rewriting in my .htaccess file? I'd like to do some URL rewriting (Why? See this question.) so that instead of users seeing addresses like labouseur.com/course-compilers.html
I'd like to do some URL rewriting (Why? See this question.) so that instead of users seeing addresses like
labouseur.com/course-compilers.html
they can instead see and use simply
labouseur.com/course-compilers
(Even better, maybe I should restructure that so that it's courses/compilers.)
I'm using a Linux-based shared hosting service for my website, so I do not have administrative control of the server, but I do have control over .htaccess. The references I've read online seemed less than clear to me, so I'm looking for a little clarity and advice here.
More posts by @Courtney195
4 Comments
Sorted by latest first Latest Oldest Best
As addition to above answers one can use
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([w-]+)$ .html [L]
([w-]+) Allows letters, digits with a hyphen (-).
(.*) will rewrite every request regardless, which may impotent other following rules causing internal server error on accessing.
You don't have to use an .htaccess file to accomplish this. Simply structure your files into directories, and instead of using "labouseur.com/course-compilers.html", just name the file index.html and put it into a directory called "course-compilers". Then when you point to that directory, the index is served up by default but will not be displayed in the URL. So you could just write your links as "labourseur.com/course-compilers" and all will be well.
No .htaccess required.
First, make sure apache has the module and that it is allowed in .htacces files, then add:
RewriteEngine on
# Rewrite rules
RewriteRule ^/myoldsubfolder/(.*) /newfolder/
Your example would require
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI}.html -f
RewriteRule ^/(([a-zA-Z0-9_]+)/?)$ /.html
For a single-level directory structure and
RewriteRule ^/(([a-zA-Z0-9_]+)/?)+$ /.html
For a multi-level structure.
But be careful
If you have the structure
/site/.htaccess
/site/subfolder/.htaccess
/site/subfolder/cats/.htaccess
The rewrite rules in the child .htaccess files will be relative to their parent folder and you can get into some messy rewrite loop situations. Make sure you use RewriteCond to stop them happening if you can!
You could actually remove .html from the files on your server and set settings in htaccess so that they get served up as html files, but that's probably not what you're looking for.
Do this :
RewriteEngine on
# Check this condition to ensure that it's not a directory.
RewriteCond %{REQUEST_FILENAME} !-d
# Check this condition to ensure that there's a file there
RewriteCond %{REQUEST_FILENAME}.html -f
# Replace html with your file extension, eg: php, htm, asp
RewriteRule ^(.*)$ .html
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.