0

I have this url:

www.mysite.it/index.php?page=search&sOrder=dt_pub_date&iOrderType=desc&sPattern=MOTO&sCity=Pisa&sPriceMin=&sPriceMax=

It is a url that results from a search button (MOTO in Pisa city).

Is it possible to rewrite the words ?page=search, sOrder, dt_pub_date, iOrderType, sPattern. sCity, sPriceMin, sPriceMax so that the url becomes:

www.mysite.it/toscana/pisa/cerca=MOTO

or just more user friendly?

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • 1
    Where does `toscana` come from? It's not mentioned anywhere in your source URL. – PP. Sep 24 '13 at 13:53

2 Answers2

0

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^page=search&sOrder=dt_pub_date&iOrderType=[^&]*&sPattern=([^&]*)&sCity=([^&]*)&sPriceMin=[^&]*&sPriceMax
RewriteRule ^index\.php$ /toscana/%2/cerca=%1? [R=301,L,NE,NC]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • This, of course, assumes that the parameters are always provided in the same order in the URL. – PP. Sep 24 '13 at 13:58
  • Work, but the search function not appear, url is right but the content are all products and not just the pattern searched (MOTO in example) Any other solution? I'm very frustated, sorry for my bad English, I hope you understand equally – Samuele Catarsi Oct 07 '13 at 15:19
  • What URL do you see in your browser after redirect? – anubhava Oct 07 '13 at 15:22
  • http://mysite.it/toscana//cerca=moto toscana is a italy's region so i'll change the word in htaccess string with %2 like the example, but not work equally – Samuele Catarsi Oct 07 '13 at 15:27
  • Hmm ok and what was the original URL that you entered before this redirect happened? – anubhava Oct 07 '13 at 16:00
  • www.mysite.it/index.php?page=search&sOrder=dt_pub_date&iOrderType=desc&sPattern=MOTO&sCity=Pisa&sPriceMin=&sPriceMax= --> if i use advance research button with MOTO as pattern www.mysite.it/index.php?page=search&sPattern=MOTO&sRegion=&sRegion=&sCategory= --> if i use normal research button with MOTO as pattern – Samuele Catarsi Oct 07 '13 at 16:10
  • First one will get redirected to: `/toscana/Pisa/cerca=MOTO` as per my rule. 2nd rule doesn't have `sCity` parameter and hence my rules doesn't kick in. You never mentioned case of missing `sCity` from URL. – anubhava Oct 07 '13 at 16:42
  • Yes all right, can i give you my website link? So you can see how it work urls, because it is a dinamic url search, wait for an answer – Samuele Catarsi Oct 07 '13 at 17:21
0

I would suggest something along the following lines. Start with capturing the parts you want into the environment:

# find sCity component, write it to environment variable
RewriteRule ([?&]sCity=([^&?]+)) $1 [E=sCity:$2]

# find sPattern component, write it to environment variable
RewriteRule ([?&]sPattern=([^&?]+)) $1 [E=sPattern:$2]

Then do a re-write using those environment variables you created from captures.

# do substitution from environment variables
RewriteRule ^(.*?)/.*$ $1/%{ENV:sCity}/cerca=%{ENV:sPattern}

Note: untested - you can try this to see if it inspires you or is successful for you. Otherwise do what I did and keep reading the mod_rewrite documentation.

PP.
  • 10,764
  • 7
  • 45
  • 59