views:

50

answers:

2

What I'm trying to accomplish, is to have people who go to: http://www.mydomain.com/$SEARCH-QUERY/$PAGE-NUMBER to redirect to search.php?query=SEARCH-QUERY&page=PAGE-NUMBER

I just figured out how to install nginx and configure php-fpm, mysql and everything, but now I'm a bit confused over the rewrites. Here's what I have, that doesn't appear to work correctly:

the php script will automatically assume it's the first page if no page query is sent with it, also I had to do two rewrite rules as I couldn't figure out how to implement it on one line so that it can have it with and without the trailing slash.

rewrite ^/search/(.*)$ /search.php?query=$1; 
rewrite ^/search/(.*)$/ /search.php?query=$1;

and then for items viewed with paged results

rewrite ^/search/(.*)/(.*)$ /search.php?query=$1&page=$2;
rewrite ^/search/(.*)/(.*)$/ /search.php?query=$1&page=$2;

If anyone could please help, I'd appreciate it greatly!

+1  A: 

You have a basic error in your regexes.

^/search/(.*)$/ will never match, because $ means "end of string". You cannot have / after the end of string.

If you need optional /, there is a ? modifier in regexes.

So, try /search/(.*)/?$.

squadette
A: 

Also, your first regex ^/search/(.*)/?$ will match all requests starting with /search/: it will push a page number into the search string, and also because * includes the empty string, it will match null search strings. If the second regex ^/search/(.*)/(.*)/?$ comes later, it will never get matched. If you want to capture the string up to the first slash, use ^/search/([^/]+)/?$ and ^/search/([^/]+)/([^/]+)/?$ (so the wildcards match strings of characters not including / instead of strings of all characters, and the + matches one-or-more instead of any-number).

pjmorse