.htaccess with variables for user friendly urls

Go To StackoverFlow.com

1

I got these two urls:

/portfolio/stamped_concrete

/p_details.php?id_cat=23&?id=91

I want to make the second url rewrite to: /portfolio/stamped_concrete/23/91

stamped_concrete is a dynamic url which is why I'm at a loss of how to solve this. Also the two files (portfolio.php and p_details.php) are in the same directory if that matters.

How would I accomplish this?

EDIT:

stamped_concrete is also a variable string that I rewrote before and it works:

RewriteRule ^services/([a-z0-9_-]+)$    /services.php?url=$1    [NC,L,QSA]

so how would I call it within the RewriteRule?

would this be on the right track?

RewriteCond %{QUERY_STRING} url=([a-z0-9_-]+)
RewriteCond %{QUERY_STRING} id_cat=([0-9]+)
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule /p_details.php?.* /portfolio/$1/$2/$3
2012-04-03 21:15
by Milkman7
Just a comment -- you have an extra question mark in your query string (could be typo in this post i suppose) -- make sure that there is only ONE q-mark which separates ALL the get vars from the rest of the URI. No need to put one after the ampersand - Kasapo 2012-04-03 21:54
Oh yea that was a typo - Milkman7 2012-04-04 00:12
I figured. Also, could you provide an example request URI? It would be nice to see the path that is requested initially. I'm not sure what you mean by dynamic URL for "stamped_concrete" ... if you could explain that a little. You can chain URL Rewrite rules together, but you have to be careful so you don't infinitely rewrite the URL (apache generally detects this so you don't hang the process) and so that the URL is rewritten correctly. But yes, on the right track I think - Kasapo 2012-04-04 14:53


0

Try this:

RewriteCond %{QUERY_STRING} id_cat=([0-9]+)
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule /p_details.php?.* /portfolio/stamped_concrete/$1/$2

Might need to tweak it a bit -- not sure if the RewriteRule part is correct (sorry).

But, the important part is QUERY_STRING, see the apache docs: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

Also, from http://wiki.apache.org/httpd/RewriteQueryString

I always mix up the backreferences, so try it out, it's eiher dollar signs or percent signs (i really thought it was dollar signs...)

(QUOTE)

Making the Query String Part of the Path

Take a URL of the form http://example.com/path?var=val and transform it into http://example.com/path/var/val. Note that this particular example will work only for a single var=val pair containing only letters, numbers, and the underscore character.

RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^/path /path/%1/%2?

(END QUOTE)

So you could probably just say "RewriteRule ^/p_details.php /portfolio/%1/%2/%3"

2012-04-03 21:52
by Kasapo
Thanks for the help. After trying several methods, this worked for me:

RewriteRule ^portfolio/?([^/]*)/?([^/]*)/?([^/]*) /p_details.php?id_cat=$2&id=$3 [QSA,NC,L]Milkman7 2012-04-04 18:35