Replacing underscores with slashes in htaccess

I’m curently working on a very old site. The site contains a lot of “static” php pages.

A group of static pages is named like this:

program_programnameX.php
program_programnameY.php
program_programnameX_subpage.php
program_programnameZ.php
They are all located in the root map of the website (I know, dirty, but like I said: old site!)

Now I want to make the url’s a bit more readable like: http://www.mysite.com/program/programnameX/ http://www.mysite.com/program/programnameY/

Both the old and new links should still be able to work and I only want to affect the pages starting with “program_”.

This is the htaccess code I came up so far:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(program_)([a-z\\-]*)$ $1$2.php [QSA,L]
But I can't seem to get it working.

Any ideas?

Shad,

ARGH! Reverse your thinking as YOU create the links in the format you want visitors to see then YOU create the mod_rewrite code to turn that into something Apache can serve.

What your code SHOULD be attempting is replacing /'s with _'s, checking for -f then redirecting to the serveable format (without the 301 which would cause the redirection to be seen). However, USE the R=301 flag to test so you can see that the mod_rewrite code is working.

Regards,

DK

Thanks for getting me on track. I now have 2 parts of code that work seperatly ok, but when I combine them I get an error saying there are too many redirects.

RewriteEngine on

#if the user brows to the site with an old url (program_foo.php) redirect him to a clean url (http://localhost/program/foo)
RewriteRule ^program_([a-z\\-]*).php$  http://localhost/program/$1  [R=301,L]

#if the user uses a clean url, tell the server what the actual filename is
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^program/([a-z\\-]*)$ http://localhost/program_$1.php

Not sure what’s causing this because I’m a complete htaccess noob.

Your point of naming the files the correct way is valid, although this site I’m working on is very old. And I’m just searching for a quick patch untill the new website launches this summer.

Shad,

Regards,

DK

Thanks a lot for pointing that out!

Shad,

That question was asked of me 2-3 years ago and the answer (actually, the one I came up with and a correct one) is discussed with the code (for both) in the tutorial linked in my signature. In other words, what you’ve attempted CAN be done but it’s slightly abusive of the server and MUST be done correctly (to avoid the looping). Let me know if you have any questions on either solution.

Regards,

DK