Forum Moderators: phranque

Message Too Old, No Replies

Clean URLs? /blah.html --> /blah/

         

john5000

10:07 pm on Jan 25, 2007 (gmt 0)

10+ Year Member



Can someone please tell me what to put in my .htaccess file so that all of my webpages ending in .html are shown without the .html.

For example I want

www.example.com/blah.html to look like www.example.com/blah/

wideaware

12:31 am on Jan 28, 2007 (gmt 0)

10+ Year Member



For example I want

www.example.com/blah.html to look like www.example.com/blah/

I'm a newbie myself, but I don't know if this will work, as /blah/ will also mean the directory "blah". Perhaps changing the / to something else.... but I could be wrong - I'm a total newbie to the .htaccess.

ahmedtheking

12:59 am on Jan 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Like this:

RewriteRule ^(.*)$ [yoursite...] [L]

It basically takes anything (that's the .* where . = any char and * = 0 or more) and adds .html on the end of it.

ahmedtheking

12:59 am on Jan 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ps have a read of: [httpd.apache.org...]

Looks like a lot, but if you go through it slowly, you'll get it.

denyer

10:13 pm on Feb 13, 2007 (gmt 0)

10+ Year Member



You could give this a whirl...

Options -MultiViews +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{HTTP_HOST} ^(.*)example\.com$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ (.*)\.html
RewriteRule ^([^.]+)\.html$ http://example.com/$1 [QSA,R=301]

At least it seems to work well enough with .php for me.

If there's a request for a filename without an extension (and that doesn't describe an existing file/directory) the content for *.html is served. The second rewrite rule forcibly rewrites something like http://example.com/page.html to http://example.com/page and thus feeds back into the first rule.

It assumes the .htaccess is placed at root; you'd need to change the last line if that isn't the case. (Or possibly use RewriteBase; I don't take this route because I use an offline server to test on that has directories for several sites in its root directory.)