Forum Moderators: phranque

Message Too Old, No Replies

Search.php friendly urls

         

Gabri

1:19 pm on Apr 17, 2009 (gmt 0)

10+ Year Member



Hello, I have a website with a search form.

<form action='<?php echo get_home_link(); ?>buscar.php' method='GET'>

When I do a search I am sent to:

/buscar.php?params=search&q=query

but I would like to be sent to

/query.html

I have this .htaccess, but it doesn't redirect when I click the search button:

Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?params=search(&.*)?$ [NC]
RewriteCond %{QUERY_STRING} ^(.*&)?q=([^&]+)(&.*)?$ [NC]
RewriteRule ^buscar\.php$ /%2.html? [R=301,L]

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^(.+)\.html$ buscar.php?q=$1&params=search [QSA,L]

can somebody help me?

Thank you in advance.

Gabriel

[edited by: jdMorgan at 2:42 pm (utc) on April 17, 2009]
[edit reason] Disabled graphic smilies in code. [/edit]

nowpc

1:34 pm on Apr 17, 2009 (gmt 0)

10+ Year Member



Hi Gabriel,

A quick thought, what about changing the "action" of the form to go to the new file? So:

<form action='query.html' method='GET'>

Although you'd need the .html file to be able to handle a query string. I know this is a rough and ready thought, but is this not possible?

Gabri

1:41 pm on Apr 17, 2009 (gmt 0)

10+ Year Member



Well, what i mean by query.html is "what_you_search".html. It's a dinamic file, I don't know if it's called like that.

Let's say i put on the search field the word "bikes", I want to be redirected to bikes.html but really what it's doing is buscar.php?params=search&q=bikes

Sorry for my english, I hope you understand.

Thank you for your answer.

jdMorgan

2:41 pm on Apr 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want to 'be sent to' <query-string>.html, then that is the URL that you should GET in your form. Use php to insert the search query as the html page name after URL-encoding all special characters in conformance with RFC2396 (there's a PHP function to do this easily, if I recall correctly).

You should never link to a URL that you *know* will get redirected, since that requires your visitor's browsers (and search robots) to make two HTTP requests. And in this case, some browsers will throw scary-looking warnings that the form submission is being redirected, and some users may cancel at that point.

You can simplify your RewriteCond QUERY_STRING patterns. For example the second one can be simplified to

RewriteCond %{QUERY_STRING} &?q=([^&]+)&? [NC]

as long as you change the back-reference in the rule from %2 to %1.

Using this un-anchored pattern will actually result in faster processing, since it allows the query to be matched left-to-right, instead of starting at the right end and backing off one character at a time (due to the initial greedy ".*" subpattern.)

Jim

Gabri

3:56 pm on Apr 17, 2009 (gmt 0)

10+ Year Member



Hello, first of all thank you both for your answers but I am trying to learn and there are a lot of things I dont understand.

I think you are asking me to change the action on the form but I also think the search function is on buscar.php

Is there any way to do it with a similar htaccess code i put on OP?

Thank you again

jdMorgan

2:06 am on Apr 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have to link to the URL you want to use on the Web. This is the URL that will appear in the browser address bar and in search engine results. Mod_rewrite cannot change this URL. So change your form to link to "example.com/search/search+terms+go+here" or something similar. Because the parameters will be passed in the URL instead of in a query string, you can use either a POST or a GET method -- It won't really matter.

Once the request for that "example.com/search/search+terms+go+here" URL arrives at your server, use mod_rewrite to internally rewrite it into a call to your buscar.php script, moving the "pieces" of the URL into the buscar.php query string:

"example.com/search/search+terms+go+here" --rewrite to--> "/buscar.php?params=search&q=search+terms+go+here"


RewriteRule ^search/(.+)$ /buscar.php?params=search&q=$1 [L]

In other words, the approach used in your rule above was exactly backwards.

For more information on this subject, see the thread Changing Dynamic URLs to Static URLs [webmasterworld.com] and other threads in our Apache Forum Library [webmasterworld.com].

Jim