Rewrite Based on File Name

Topics: Developer Forum
Nov 23, 2009 at 2:25 PM

Ok - I want to have my rewrite work in this manner

 

Incoming /page/whatever-file-name-12345.cfm 

Return page/page.cfm?PageID=12345

 

The key is pulling the *12345 out of the file name.. any suggestions?

 

Thank you in advance

Nov 23, 2009 at 5:36 PM

What are some examples of file names and how different are they likely to be?

Are they *always* going to end in 5 digits?

Will there be any digits in the file names themselves?

You could write a RegExp that picks up on the last -nnnn.cfm where nnnn is any number of digits (but at least one). Would that work?

Nov 23, 2009 at 5:41 PM

The final -12345 is the actual "pageID" value - it could be 123 or it could be 123495959  It is just an id value

So the title is the first part of the filename and the Page ID is the last part ....... '-' PageID.cfm

The-Title-Of-article-PAGEID.cfm

So I need to grab the final PAGEID

Nov 23, 2009 at 5:47 PM

How about the following:

RewriteRule ^/page/.*-([0-9]+)\.cfm$ page/page.cfm?PageID=$1

In English, (hopefully) this reads any URL that starts with /page/ then has zero or more of any digit followed by a dash followed by one or more numbers from 0-9 rewrites to page/page.cfm?PageID= followed by the digits captured above. REmember to test using the testdriver.exe file... I haven't tested this.

You may need to make the .* ungreedy by changing it to .*? (I think), but testing will show you this.

Also, don't forget to add any flags at the end that you may need eg. [I,L] for case Insensitive and Last rule to be processed.

HTH

shonk

Nov 23, 2009 at 5:54 PM

RewriteRule ^/page/.*-([0-9]+)\.cfm$ page/page.cfm?PageID=$1

 

^/page/ = Anything with this in it

.* = any filename part (could be anything from About-Us to "something-really-long-that-does-not-matter"

-([0=9]+)\ = ID Number ($1)

 

 

 

Nov 23, 2009 at 5:57 PM
QDesign wrote:

RewriteRule ^/page/.*-([0-9]+)\.cfm$ page/page.cfm?PageID=$1

 

^/page/ = Anything with this in it

.* = any filename part (could be anything from About-Us to "something-really-long-that-does-not-matter"

-([0=9]+)\ = ID Number ($1)

 

 

 

That last line should read:

-([0-9]+) = - then ID number ($1)

then

\.cfm$ = .cfm (end of line)