How do I implement URL rewriting in my .htaccess file?
.htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about url-rewriting, htaccess, , , .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :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.
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 ^(.*)$ $1.html
First, make sure apache has the module and that it is allowed in .htacces files, then add:
RewriteEngine on
# Rewrite rules
RewriteRule ^/myoldsubfolder/(.*) /newfolder/$1
Your example would require
RewriteCond %REQUEST_URI !-d
RewriteCond %REQUEST_URI.html -f
RewriteRule ^/(([a-zA-Z0-9_]+)/?)$ /$1.html
For a single-level directory structure and
RewriteRule ^/(([a-zA-Z0-9_]+)/?)+$ /$1.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!
As addition to above answers one can use
RewriteEngine on
RewriteCond %REQUEST_FILENAME !-d
RewriteCond %REQUEST_FILENAME.html -f
RewriteRule ^([w-]+)$ $1.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.
Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.
Comments
Post a Comment