Rewrite mods with two GET variables - php

I need to change url from mydomain.com/search.php?page=1&postcode=xxx
to: mydomain.com/search/1/xxx
I know how to change url from mydomain.com/search.php?page=1 to mydomain.com/search/1
This can be done with this:
RewriteRule ^search/([0-9]+)/?$ search.php?page=$1 [NC,L]
How can I add another GET to my url?
Thank you!

This should work:
RewriteRule ^search/([0-9]+)/(.*)$ search.php?page=$1&postcode=$2 [NC,L]

Related

Multiple vars in .htaccess

I want create a user's page, like twitter, who have more options, like: site.com/u/User but i want create multiples, like: site.com/u/User, site.com/u/User/photos, i try it:
RewriteRule ^u/(.+)$ perfil/?user=$1 [L,QSA]
RewriteRule ^u/(.+)/t$ perfil/?user=$1&t=1 [L,QSA]
But it doesn't works, anyone can help me?
Forgot, i founded it, lol, i found here http://roshanbh.com.np/2008/03/url-rewriting-examples-htaccess.html
Just change (.*) to ([a-zA-Z0-9_-]+)

ApiRest Beautify url with rewrite paramaters

I'm building a RestApi and I have a problem with urls. I want to transform "api.php?request=users" into "api/users". But I also want it to be dinamic. For example: If a user types "api/users/13" I want it to be tha same as "api.php?request=users&id=13". Is there any way to make it dinamic? Do I have to create every case individually? How could this be done in each of these two cases? Thanks so much in advance for your help!!
the best way is change .htaccess file like this:
RewriteEngine On
RewriteRule ^api/([^/])/([^/])/([^/]*)$ /api.php?request=$1&id=$2&state=$3 [L]
The original URL:
http://www.domain.com/api.php?request=mike&id=3&state=active
The rewritten URL:
http://www.domain.com/api/mike/3/active
update: if you don't know the number of parameter add one more rewrite for each 'level':
RewriteRule ^api/([^/])/([^/])/([^/])$ /api.php?request=$1 [L]
RewriteRule ^api/([^/])/([^/])/([^/])$ /api.php?request=$1&id=$2 [L]
RewriteRule ^api/([^/])/([^/])/([^/]*)$
/api.php?request=$1&id=$2&status=$3 [L]
Regards.
I think you must add a new RewriteRule, because you need different variables.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)/(.*)/?$ $1.php?request=$2
RewriteRule ^(.*)/(.*)/(.*)/?$ $1.php?request=$2&id=$3

URL Rewriteing from dynamic links

I'm not sure how to re-write a dynamic url into another kind of dynamic url?
I'm looking to change:
/blog/category.php?catseourl=ExampleCat&pn=1
/blog/category.php?catseourl=ExampleCat&pn=2
/blog/category.php?catseourl=ExampleCatTwo&pn=1
Into:
/blog/category/ExampleCat/1
/blog/category/ExampleCat/2
/blog/category/ExampleCatTwo/1
I've got to this point:
RewriteRule ^blog/category/([^/]*)$ /blog/category.php?catseourl=$1&pn=$1 [L]
But I'm not sure how to add the dynamic changes into the left hand side of the rewrite?
Thanks for any help.
Try
RewriteRule ^blog/category/([^/]+)/([0-9]+)/?$ /blog/category.php?catseourl=$1&pn=$2 [L]
RewriteEngine On
RewriteRule ^blog/category/([^/]*)/([^/]*)/?$ /blog/category.php?catseourl=$1&pn=$2 [L]
Try this:
RewriteRule ^blog/category/(.+)/([0-9]+)$ /blog/category.php?catseourl=$1&pn=$2 [L]

Retain query string on rewrite .htaccess

I currently have a simple rewrite that redirects
/photos/2
to
/photoviewer.php?photo=2
However I need to be able to allow the user to add a photo to their cart so I was thinking I need to retain the query string so that the following works.
/photos/2?action=purchase
redirects to:
/photoviewer.php?photo=2&action=purcahse
My current htaccess rule is:
RewriteRule ^photos/([a-zA-Z0-9_-]+)$ photoviewer.php?photo=$1 [L]
Append the [QSA] flag (query string append).
RewriteRule ^photos/([a-zA-Z0-9_-]+)$ photoviewer.php?photo=$1 [L,QSA]
In your rule, replace [L] with [L,QSA].
That will then retain the query information.
Hope this helps!
[QSA,L] instead of just [L] should do the trick. Hope this helps!
How about just extending the idea to pass "any" name/value pairs thru the URL to the photoviewer.php script? This is an elegant approach I've used in the past.
RewriteRule ^photos\/([^/\.\-]+)\/([^/\.\-]+)$ /photoviewer\.php\?$1=$2 [L]
RewriteRule ^photos\/([^/\.\-]+)\/([^/\.\-]+)\/([^/\.\-]+)\/([^/\.\-]+)$ /photoviewer\.php\?$1=$2&$3=$4 [L]
RewriteRule ^photos\/([^/\.\-]+)\/([^/\.\-]+)\/([^/\.\-]+)\/([^/\.\-]+)\/([^/\.\-]+)\/([^/\.\-]+)$ /photoviewer\.php\?$1=$2&$3=$4&$5=$6 [L]
So, something like:
photos/photo/2/action/purchase
rewrites to:
photoviewer.php?photo=2&action=purchase

Rewriting URL rule, example like StackOverflow

What would be the apache rewrite url to extract the id number (434376) from this url?
http://stackoverflow.com/questions/434376/unique-url-slug
something like
RewriteEngine On
RewriteRule ^questions/([0-9]+)/?$([A-Za-z0-9-]+) post.php?post_id=$1 [NC,L]
...but it doesnt work on my site...any tips?
thanks
Tested:
RewriteRule ^questions/([0-9]+)/?.*$ rewrite.php?post_id=$1 [NC,L]
Well if the / is optional it probably should be:
RewriteRule ^questions/([0-9]+)/?([A-Za-z0-9-]+)?/?$ post.php?post_id=$1 [NC,L]
Let me know if it works..

Categories