Forum Moderators: phranque

Message Too Old, No Replies

Parameter to URL and back

How to make URL from PHP parameters and back

         

PetNov

3:58 pm on Jun 21, 2011 (gmt 0)

10+ Year Member



Hi,
I would like to use .htaccess (and regex) to accomplish this:

After typing query into searchbox and pressing enter, the url is:

http://subdomain.domain.com/index.php?media=0&query=money&search=1

but address bar in browser is (for user and google) showing this:

http://subdomain.domain.com/money

AND

after typing in address bar in browser

http://subdomain.domain.com/money

it actually goes to

http://subdomain.domain.com/index.php?media=0&query=money&search=1

but in address bar in browser still showing

http://subdomain.domain.com/money

I tried everything but no success. Eg. this:

RewriteCond %{QUERY_STRING} ^query=(.*)$ [NC]
RewriteRule ^/index.php$ /%1 [NC,L,R=301]


Pls, could you help? Thank you very much.

g1smd

7:35 pm on Jun 21, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Check recent threads as this is a question that has been asked and answered several times per month for the last decade.

The basics are that you will need to test
THE_REQUEST
with a preceding
RewriteCond
when redirecting parameter based requests to SEF URLs. The redirect target must include the domain name and the
[R=301,L]
flags.

You'll need a separate rule for the rewrite. It accepts the SEF URL request and maps it to an internal server filepath. The target does not include a domain name, and only the
[L]
flag.

You should link only to the SEF URLs from within the site.

Be aware that you cannot redirect POST requests. This is a security feature that you cannot override. So, if you're submitting a form, this cannot be done.

PetNov

8:49 am on Jun 22, 2011 (gmt 0)

10+ Year Member



Oh, thanks for info about not redirecting POST request, I did not know this...

PetNov

4:27 pm on Jun 23, 2011 (gmt 0)

10+ Year Member



So, thank you again for help. Now, I have everything done, except one thing: URL encoding... If there is some diacritics in a address, it fails. Now, my code is:

Rewritecond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?([^\ ]+)query=(.*?)&([^\ ]+)start=(.*?)&([^\ ]+)\ HTTP/
Rewriterule ^index\.php$ http://subdomain.domain.com/%2/%4? [R=301,L]

RewriteRule ^([a-z]+)/([^/]*)$ /index.php?parameter=0&query=$1&anotherpar=1&start=$2&lastpar=-1 [L]


If there is href to e.g.:

http://subdomain.domain.com/index.php?parameter=0&query=šèø&search=1

redirections fail. In browsers address bar it shows

http://subdomain.domain.com/%25C5%25A1%25C4%258D%25C5%2599

I tried hardto find solution, on this forum too but no success. Thank you for advices.