.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

http://domain.com/news 

and

http://domain.com/news/story/story-title

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

http://domain.com/news/index.html

and

http://domain.com/news/story-title20101211.html

I thought I could use
[/SIZE][/FONT]

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

[FONT=Arial][SIZE=2]

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.

Thanks,
Steven
[/SIZE][/FONT]

Steven,

[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.

Regards,

DK

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

JR,

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

Regards,

DK