Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

URL rewrite

Status
Not open for further replies.

chrissparkle

Programmer
Mar 27, 2006
50
NZ
I have a url which looks like this: /f-search_results.cfm?type=online?page=1. I've been able to write a rewrite rule for it:

RewriteRule /search/singles-(.*)\.htm /f-search_results.cfm\?type=$1 [I,O]

this works for "type=online" part of it, but how do i modify the rule so that the "page=1" can be included in the rule?

Ideally I'd like the rewritten rule to look like:

/search/singles-online-1.htm - the 1 being the page number.

Can someone help me out there please..
 
Hi

chrissparkle said:
/f-search_results.cfm?type=online[red]?[/red]page=1
I assume that the second question mark ( ? ) is just a typo.
Code:
RewriteRule /search/singles-([^-]*)-([0-9]*)\.htm /f-search_results.cfm?type=$1&page=$2 [I,O]

Feherke.
 
Sometimes the second URL parameter won't be there, is it possible to have this rule work so that it displays "/search/singles-online-1.htm" if the 2nd URL param is there, and "/search/singles-online.htm" if it is not?

 
Thanks for this, but the below code:

Code:
RewriteRule /search/singles-([^-]*)(-([0-9]*))?\.htm /f-search_results.cfm\?type=$1&thepage=$2 [I,O]

thinks that the second URL variable is "-1" rather than just "1". It includes that hyphen in the variable?
 
SOrry about that. I tried it exactly as you wrote, but neither "/search/singles-online.htm" or "/search/singles-online-1.htm" is working at all. None of these URLs seem to find the rewrite rule?
 
It was actually more problem in my code than the rule sorry. Can I ask why the it is 3 instead of 2? And if i wanted to add a third possible url paramater, what would be the way?
 
Hi

chrissparkle said:
Can I ask why the it is 3 instead of 2?
Yes, but at this point would be better for you to learn about regular expressions.
Code:
/search/singles-[red]([^-]*)[/red][green](-[/green][blue]([0-9]*)[/blue][green])[/green]/?\.htm
                 [red]\___/[/red]  [green]\_[/green][blue]\____/[/blue][green]/[/green]
                   [red]1[/red]      [green]2[/green] [blue]3[/blue]
                  
/f-search_results.cfm?type=[red]$1[/red]\&thepage=[blue]$3[/blue]
By the way, you do not have to escape the question mark ( ? ) in the replacement string. But I forgot the you have to escape the ampersand ( & ) because it means the entire matched string.

I suggest to read this short introduction, entire site, Perl improvement, doc with examples or just google for regular expressions.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top