Search form redirection

Discuss practical ways rearrange URLs using mod_rewrite.

Search form redirection

Postby Gabri » Fri Apr 17, 2009 8:24 am

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
Gabri
 
Posts: 4
Joined: Fri Apr 17, 2009 8:22 am

Postby richardk » Fri Apr 17, 2009 8:45 am

Works fine for me.

Does any mod_rewrite work? Try this mod_rewrite test.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Gabri » Fri Apr 17, 2009 8:49 am

Hello, the test works perfectly. The problem can be on buscar.php? Can it be stopping the redirection?

If i go to /query.html directly I see what I have to see. The problem is the form. I would like it to go directly to /query.html
Gabri
 
Posts: 4
Joined: Fri Apr 17, 2009 8:22 am

Postby richardk » Fri Apr 17, 2009 9:12 am

What happens if you go directly to /buscar.php?params=search&q=query (ie. copy and paste it into the browser address bar)?

The problem can be on buscar.php? Can it be stopping the redirection?

No. But you could try doing it for a different file anyway.

If i go to /query.html directly I see what I have to see. The problem is the form. I would like it to go directly to /query.html

So this part is working?
Code: Select all
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^(.+)\.html$ buscar.php?q=$1&params=search [QSA,L]


Try replacing
Code: Select all
^buscar\.php$

with
Code: Select all
buscar\.php$
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Gabri » Fri Apr 17, 2009 9:27 am

richardk wrote:What happens if you go directly to /buscar.php?params=search&q=query (ie. copy and paste it into the browser address bar)?


Exactly the same as if I was just putting: /query.html
I see the same page, but it's not redirected.

So this part is working?

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


I suppose. What is working is that /query.html is the same page as /buscar.php?params=search&q=query. What isn't working is that I don't want to have /buscar.php?params=search&q=query. I would like it to redirect to query.html

Try replacing
Code: Select all
^buscar\.php$

with
Code: Select all
buscar\.php$


It didn't worked. The result is exactly the same.

Thank you again for your help.
Last edited by Gabri on Fri Apr 17, 2009 11:16 am, edited 1 time in total.
Gabri
 
Posts: 4
Joined: Fri Apr 17, 2009 8:22 am

Postby Gabri » Fri Apr 17, 2009 11:03 am

Hello, I have solved it this way:

Code: Select all
Options +FollowSymLinks
RewriteEngine On

RewriteRule ^juegos-de-(.+)\.html$ buscar.php?q=$1&params=search [QSA,L]


The form is like this now:

Code: Select all
<form action='<?php echo get_home_link(); ?>redirect.php' method='GET'>


And redirect.php is:


Code: Select all
<?php
$name = $_GET['q'];
header('Location: /juegos-de-'.urlencode($name).'.html');
?>


I suppose it's not the best way to do it but it seems to work.

Thank you again
Gabri
 
Posts: 4
Joined: Fri Apr 17, 2009 8:22 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 15 guests

cron