Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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.

Thanks!

share|improve this question

4 Answers 4

up vote 10 down vote accepted

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
share|improve this answer
    
Looks great. Thanks. I'm going to try that. –  Alan Jul 8 '10 at 20:38
    
There is more complexity here, especially if you are worried about duplicate content problems. With the rule above, your pages will still be accessible from the old URLs. –  JasonBirch Jul 9 '10 at 22:12

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!

share|improve this answer
    
Do you mind going into more detail on that last point (using RewriteCond to stop the loops)? –  tnorthcutt Jul 9 '10 at 13:30

This page has always been the easiest intro for me, the the site is beautiful.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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