.htaccess adding .html to CMS urls

[FONT=Arial][SIZE=2]I’m working on a project at the moment migrating an existing website static HTML website to a CMS.

The new URL structure within the CMS produces pages like




However, the site owner wishes to keep their existing urls, so for example




I thought I could use

[FONT=Arial][SIZE=2]RewriteRule ^(.*)\\\\/$ /$1.html [R=301,L]


Although it returns a page, it also returns all news content.

Of course, creating some redirects is another option, /news/[/SIZE][/FONT]story-title20101211.html [FONT=Arial][SIZE=2]to /news/story-title

I’ve always felt these rules were a black art, so I doff my cap to anyone who can help.



[rant #1][indent]The use of “lazy regex,” specifically the :kaioken: EVERYTHING :kaioken: atom, (.*), and its close relatives, is the NUMBER ONE coding error of newbies BECAUSE it is “greedy.” Unless you provide an “exit” from your redirection, you will ALWAYS end up in a loop![/indent][/rant #1]

That’s the gist of it although your code is specific to Apache 2.x and has an escaped backslash REQUIRED before the REQUIRED trailing slash. IMHO, proper regex will easily solve the problem of redirecting \.html file extensions to the same without the file extension BEFORE your CMS takes over and serves the proper file from the database.



I was trying to write a way to help, LOL but then the way they write the story title with a dynamic title is confusing :\ So this is just a guess LOL

RewriteRule ^/news/story-title([\\d+])$ news/story-title$1\\.html


Good guess … but:

  1. The / after the start anchor (the ^) is ONLY matched by Apache 1.x (or in server/virtual host configuration files).

  2. The story-title is “dynamic,” i.e., composed of characters such as [-a-z]+ (if including uppercase letters, [-a-zA-Z]+) so this must be captured in the regex as another atom and referenced in the redirection, too.

  3. I think that you forgot the Last flag which will terminate this round of mod_rewrite processing - but, if the CMS’s mod_rewrite follows, LEAVE THE Last FLAG OFF AS YOU HAVE DONE!

All in all, not a bad effort, though.

RewriteRule ^news/([-a-zA-Z]+\\d+)$ news/$1.html