Duane Lortie Duane Lortie -4 years ago 81
HTML Question

Getting pretty URLs with a GET form

I'm trying to make dynamic/pretty urls like

domain.com/p/term


In my case /p is actually a PHP script, via an .htaccess entry.

<Files "p">
ForceType application/x-httpd-php
</Files>


and
term
is a search term, used to fetch relevant data from a database

So, if the "/term" part is provided in the URL everything is good. The problem is, if the request is just "domain.com/p/" with no term, I need to show a form asking for a term, and since I want the term the viewer enters to be displayed in the URL, I thought I should use a GET form. Super basic.. or so it seems :/

<form action="/p/" method="GET">
<input type="text" name=pid>
<input type="submit">
</form>


When submitted, it results in the URL being
domain.com/p/?pid=term


Now, I just need to find how to mod rewrite that to
domain.com/p/term


I've tried numerous re-write rules and can't get the desired result...
My latest attempts...

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

#Rewrite GET search "/p?pid=term" => "/p/term"
RewriteRule ^p\?pid=(.*)$ /p/$1 [L]

#Rewrite GET search "/p/?pid=term" => "/p/term"
RewriteRule ^p/\?pid=(.*)$ /p/$1 [L]


Neither seem to be triggering the rewrite, I still get /p/?pid=term in the address bar.

I've looked over several related questions, but none seem to address this scenario. A tap with a clue stick would be greatly appreciated.

Answer Source

You can not match against QueryString (Url part after the ?) using a RewriteRule. You need to match against %{QUERY_STRING} variable

RewriteEngine on


RewriteCond %{QUERY_STRING} ^pid=(.+)$
RewriteRule ^p/?$ /p/%1? [L,R]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download