Rewrite Rules

Jan 11, 2010 at 10:43 PM
Edited Jan 11, 2010 at 10:44 PM

Let me see if I can explain this and if the rewrite filter can do what I am wanting.

 

First, I have the following sample url

http://www.xyz.com/page/This-is-a-title-1234.cfm
I want it to load /page/page.cfm?PageID=1234

Secondary
If someone goes to http://www.xyz.com/abc/account.cfm

-- The /abc/ is virtual and doesn't actually exist but holds some files that do tasks based on the /abc/ variable.  so it could be /abc/ /sss/  /xxx/ etc..

So the url: http://www.xyz.com/abc/account.cfm
I want to load /clients/account.cfm?ClientID=abc

Also - the url could be http://www.xyz.com/abc/account.cfm?mode=login&action=123
So I would want to load /clients/account.cfm?ClientID=abc&mode=login&action=123

(The url variables are numerous and unknown so it needs to be dynamic, ie there may be no url variables and there may be 20)

** just to make sure I am being clear .. /abc/ is a variable and there could be hundreds of them so I need it to be dynamic.


Is this possible and how?

Thanks in advance!!

Coordinator
Jan 11, 2010 at 10:51 PM
Edited Jan 11, 2010 at 10:53 PM

This takes care of the first part:

# http://www.xyz.com/page/This-is-a-title-1234.cfm
# I want it to load /page/page.cfm?PageID=1234

RewriteRule  ^/page/(.+)-([0-9]+)\.cfm$  /page/page.cfm?PageId=$2

But I don't know why you would include the .cfm in the publicly-exposed URL. Just make it /1234/This-is-a-title - skip the "page" and the ".cfm" in the URL. If you go this route, You'd have to rework that rule I provided.

For the second one:

# http://www.xyz.com/abc/account.cfm?mode=login&action=123
#      => /clients/account.cfm?ClientID=abc&mode=login&action=123
RewriteRule ^/([^/]+)/account\.cfm  /clients/account.cfm?ClientId=$1 [QSA]

Here again, I don't know why you want to expose .cfm as a URL path - but that's up to you. You could just use /abc/account?foo. And again, rework the rule to handle that.

Good luck

Jan 11, 2010 at 10:55 PM

Very good then.

Since right now it's flexible ... what would the rules be if I wanted it to be

http://www.xyz.com/page/1234/startpage/1

Where /page/ would make it call /page/page.cfm?pageID=1234

And /startpage/1 would make it /page/page.cfm?pageID=1234&startpage=1

then for the other piece

http://www.xyz.com/<non-existant-directory>/account/mode/login/action/123

Would load /clients/account.cfm?clientID=<non-existant-directory>&mode=login&action=123

Thanks for the QUICK response.

 

Coordinator
Jan 11, 2010 at 11:28 PM

On the first part

# /page/1234   ==>
#       /page/page.cfm?pageID=1234
RewriteRule ^/page/([0-9]+)$   ^/page/page.cfm?PageID=$1 [L]

# /page/1234/startpage/1  ==>
#       /page/page.cfm?pageID=1234&startpage=1
RewriteRule ^/page/([0-9]+)/startpage/([0-9]+)$   ^/page/page.cfm?PageID=$1&startpage=$2  [L]

For the second part

# /Foo/account/mode/login/action/123 ==>
#      /clients/account.cfm?clientID=Foo&mode=login&action=123
RewriteRule ^/([^/]+)/account/([^/]+)/([^/]+)/([^/]+)/([^/]+)$  /clients/account.cfm?clientId=$1&$2=$3&$4=$5 

But this last one is trickier.  It will also rewrite a url like /Foo/account/X/Y/A/B   into /clients/account.cfm?clientID=Foo&X=Y&A=B , which may not be what you want.  I understand that you want the client ID to be variable, but I'm not clear on the expected variability of the other segments in the URL path (mode, login, action, etc).

Jan 12, 2010 at 1:36 PM

Ok ... are the first page /page1234/startpage/1 

<startpage> could be a variable (could be anything).

On the second part ... what if there weren't any url parameters (such as mode or action) .. would the script just ignore them?

 

Coordinator
Jan 12, 2010 at 4:04 PM
StriperCrew wrote:

Ok ... are the first page /page1234/startpage/1 

<startpage> could be a variable (could be anything).

Then you'll need to change the rule to capture that segment and make it variable.

RewriteRule ^/page/([0-9]+)/([^/]+)/([0-9]+)$   ^/page/page.cfm?PageID=$1&$2=$3  [L]

On the second part ... what if there weren't any url parameters (such as mode or action) .. would the script just ignore them?

 

Not as currently written. You'd need an additional rule.

With all these examples, you can start to see how things work, right?