Another mod_rewrite question

software development

#1

How can I set up .htaccess to look at the last keyword in an URL and use it as a parameter.

For instance, if I have URLs like this:
[php]http://www.mysite.com/coins/half-cents/liberty-cap/1794-P/high-relief/2/
http://www.mysite.com/coins/half-cents/liberty-cap/1794-P/3/
http://www.mysite.com/coins/proof-sets/1937-P/4125/[/php]

The last field in the URL is an (id), which I would like to pass to a target page so the database an locate an exact coin. Notice that there are a different number of fields separated by “/” before the (id) field.

What type of re-write code would I need in order to capture only the last field and pass that info to the next page?

Thanks


#2

You can use the ending marker ‘$’…
if you always want the last bit between trailing slashes, something like this should work:

RewriteRule (/[^/]+/)$ ?parameter=$1 [L]

You might need [QSA], but I don’t think so.

I’m just learning, so could be wrong, but I use something similar to capture parts of the URL and pass it as part of the query string.


#3

Awesome, thanks for the help!


#4

Sorry, looking at that again, I think you need something like:

RewriteRule (.*)/([^/]+)/?$ $1?parameter=$2 [L]

that should take any URL and look for the last bit after the last / and an optional trailing / then paste that last bit after adding ?parameter=
again, I’m not sure, but you might need [QSA] at the end…

this assumes that you want to do this rewrite with every URL… if not, you’ll need to set some RewriteCond (itions)…