1

I have following .htacess code which allow three $_GET parameters to show in pretty manner

RewriteRule ^search/([a-zA-Z0-9-_%]+)/([a-zA-Z0-9-_%]+)/([a-zA-Z0-9-_%]+)/(.*)$ search.php?name=$1&city=$2&age=$3 [L]
RewriteRule ^search/([a-zA-Z0-9-_%]+)/([a-zA-Z0-9-_%]+)/(.*)$ search.php?name=$1&city=$2 [L]
RewriteRule ^search/([a-zA-Z0-9-_%]+)/(.*)$ search.php?name=$1 [L]

The url will look like domain.com/search/name/city/age

but I want to add more dynamic filters like domain.com/search/name/city/age?gender=male

now my .htaccess don't treat ?gender=male as normal get parameters. If I print all $_GET values in php using print_r($_GET), it will show me only $_GET values that I have written in .htaccess

What could be the solution?

Justin Iurman
  • 18,954
  • 3
  • 35
  • 54
Uday Hiwarale
  • 233
  • 3
  • 15

1 Answers1

0

You have to use %{QUERY_STRING} to match ?gender=male.
Also, you can replace your patterns by ([^/]+) (every characters except a slash /) to be more generic.

Here's an example of capturing gender with /search/name/city/age?gender=something

RewriteEngine On

RewriteCond %{QUERY_STRING} ^gender=([^&]+)$
RewriteRule ^search/([^/]+)/([^/]+)/([^/]+)/?$ /search.php?name=$1&city=$2&age=$3&gender=%1 [L]

RewriteRule ^search/([^/]+)/([^/]+)/([^/]+)/?$ /search.php?name=$1&city=$2&age=$3 [L]
RewriteRule ^search/([^/]+)/([^/]+)/?$ /search.php?name=$1&city=$2 [L]
RewriteRule ^search/([^/]+)/?$ /search.php?name=$1 [L]

EDIT: easier version with QSA flag (query string append)

RewriteEngine On

RewriteRule ^search/([^/]+)/([^/]+)/([^/]+)/?$ /search.php?name=$1&city=$2&age=$3 [L,QSA]
RewriteRule ^search/([^/]+)/([^/]+)/?$ /search.php?name=$1&city=$2 [L,QSA]
RewriteRule ^search/([^/]+)/?$ /search.php?name=$1 [L,QSA]
Justin Iurman
  • 18,954
  • 3
  • 35
  • 54
  • I will accept this answer. Thank you. Just one little problem. Url ending with `/` won't work. Any modifications? – Uday Hiwarale Aug 22 '14 at 15:23
  • I edited my answer to work with both (with or without trailing slash). The choice is up to you (but choose one, otherwise you'll have some duplicate content problem) – Justin Iurman Aug 22 '14 at 15:26
  • Working fine. Thank you. And I will appreciate if you explain me how duplication problem can occur? Just to make sure it won't happen. – Uday Hiwarale Aug 22 '14 at 15:31
  • Well duplicate content is when you have 2 different urls leading to same content. In your case, i made trailing slash `/` optional (`/?`), so `/search/name/city/age` and `/search/name/city/age/` lead to same content but one is considered as a page and the other as a folder by search engines – Justin Iurman Aug 22 '14 at 15:33
  • `RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d` will it prevent that – Uday Hiwarale Aug 22 '14 at 15:37
  • No, those conditions are for checking if it's an existing folder or file. A good practice is to decide if you want your links to be with or without trailing slash and then keep it all along. And in my code, you can replace `/?` by nothing if you don't want a trailing slash after your link (or by `/` if you want it) – Justin Iurman Aug 22 '14 at 15:40
  • Oh. Just one last question. If I have like 10 dynamic $_GET parameters to add, do I need to add all those in .htaccess code or is there any wildcard for that? – Uday Hiwarale Aug 22 '14 at 15:56
  • I think http://stackoverflow.com/questions/4071155/htaccess-rewriterule-to-preserve-get-url-parameters solution saves all these efforts – Uday Hiwarale Aug 22 '14 at 16:02
  • Yes in your case it's easier to use `QSA` flag. You can see my edited answer about it – Justin Iurman Aug 22 '14 at 21:41