0

What is the best way to do when I type a search on my site to get the url like mysite.com/searach/blabla/video instead of mysite.com/?search=blabla&type=video. Please don't tell me that I have to use CodeIgniter because it doesn't help. I am sure that all I need is a small function in PHP.

This is my search.php:

<form id="searchwrapper" name="searchform">
<input type="text" class="searchbox" id="txtSearch" name="search" alt="Search Criteria" />
<input type="submit" src="../images/empty.png" class="searchbox_submit"/>

<p id="circ">
<input  type="radio" id="mp3" name="type" checked value="mp3"/>
<label class="circ-btn normal" for="mp3">mp3</label>
                        </p>
<p id="circ">
<input type="radio" id="album" name="type" <?php echo $radio2;?> value="album"/>
<label class="circ-btn normal" for="album">Album</label></p>                
</form>

My .htacces rules are set correctly; when I access my site directly using mysite.com/searach/blabla/video, it works fine, but this is not the issue. I need to have my link like this when I type a search on my site. Instead I get this link and is not right mysite.com/?search=blabla&type=video.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Bengau
  • 175
  • 5
  • 14

3 Answers3

2

You can use mod_rewrite, and put this in your .htaccess file

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^search/([^/\.]+)/([^/\.]+)/?$ index.php?search=$1&type=$2 [L]

then use this in your php

$URI = substr($_SERVER['REQUEST_URI'], 1);
$URIparts = explode("/", $URI);

That gives you an array with the parts of the url.

Also you should try to sanitize the input for security.

Community
  • 1
  • 1
JKirchartz
  • 17,612
  • 7
  • 60
  • 88
  • JKirchartz i have updated my question with my search form, can you give me more details on how to make it work for my search form ? – Bengau Oct 07 '12 at 03:54
  • @ColaDeu Use @gllen's `RewriteCond` and `RewriteRule` ... the form will always submit using the `get` method to put the `QUERY_STRING` in the URL, @gllen's method would rewrite that showing your desired results – JKirchartz Oct 07 '12 at 14:21
  • i did it and it work how i need. but i have another issue. with the space. This function works only if search keyword contain no space. If i search BlaBla i get SEF URL but if i search Bla Bla. I get ?search=Bla+Bla&type=video. Can you tell me how to fix it in this code from gllen ? RewriteCond %{QUERY_STRING} ^search=(\w+)&type=(\w+)$ – Bengau Oct 07 '12 at 19:10
  • @ColaDeu you should be able to escape them with the [`[B]` flag](http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_b), so try: `[B,R,L]` – JKirchartz Oct 07 '12 at 23:57
  • If i type space + or - in a search keyword it gives me a query url – Bengau Oct 08 '12 at 00:37
  • @ColaDeu try adding `RewriteMap escape int:escape` to your `httpd.conf` then use `${escape:$1}` in place of `$1` and make the same change for `$2` – JKirchartz Oct 08 '12 at 13:27
  • JKirchartz i edited httpd.conf on my apache .. added the code and changed $1 and $2 from my .htaccess but still the same issue. I will open another question about this. Thanks You helped my alot. – Bengau Oct 08 '12 at 19:17
1

As others commented, you need mod_rewrite if you're using apache.

If you actually want to type in a search and end up at a SEF URL, as you described, you want something like:

RewriteCond %{QUERY_STRING} ^search=(\w+)&type=(\w+)$
RewriteRule ^ http://mysite.com\/%1\/%2? [R,L]

.. which would redirect requests such as

http://mysite.com/index.php?search=A&type=B

.. to

http://mysite.com/A/B

.. and JKirchartz provided the sample to use a php script for a SEF url, for example, having apache serve

http://mysite.com/video.php?search=A&type=B

.. behind the scenes when

http://mysite.com/A/B

.. is requested

gllen
  • 56
  • 6
  • gllen i arleady have .htaccess configurated The code that you gave me is for .htaccess too ? What code should i add in my page or search form to make it run like i wish to? – Bengau Oct 07 '12 at 13:25
0

You can use apache Mod Rewrite, though it can be frustrating for someone just learning it. Alternatively, you could move your site to a cms like drupal, wordpress, or joomla and have it automatically create an alias for you.

Matt
  • 43
  • 1
  • 7