0

I have mod rewrite enabled to remove all page extentions....

This is done in httpd.conf as I am using apache on windows

the setup I have is

<IfModule mod_rewrite.c>
   Options +FollowSymLinks
   Options +Indexes
   RewriteEngine On
   RewriteCond %{SCRIPT_FILENAME} !-d
   RewriteRule ^([^\.]+)$ $1.html [NC,L]
</IfModule>

and this makes domain.com/bookings.html

appear as domain.com/bookings

I have php enabled in html files, so it parses all pages for php

But I have now put a search box on my site, and if I search for "music" it will use the url:

domain.com/search?q=music

I would like my urls to look like:

domain.com/search/music

But also, I would like to be able to type

domain.com/search/abba

And it would load the page "search.html" lets call it "search" and it would add the parameter ?q=abba , but then still look like the above example in the URL bar

I'm sure this can be achieved with mod rewrite but I am not sure how to phrase the expression.

Thanks in advance for any help I receive :)

khr055
  • 28,690
  • 16
  • 36
  • 48
DJ-P.I.M.P
  • 105
  • 2
  • 6
  • 15

2 Answers2

0

use the kind of URLs in your html: domain.com/search/music

add this in your .htaccess file in your DocumentRoot.

Options +FollowSymLinks +Indexes -MultiViews
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_URI} (search)/([\w\d]+) [NC]
RewriteRule ^ %1.html?q=%2 [L,QSA]

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

or add this to your .conf file

<IfModule mod_rewrite.c>
   Options +FollowSymLinks
   Options +Indexes
   RewriteEngine On

    RewriteCond %{REQUEST_URI} (search)/([\w\d]+) [NC]
    RewriteRule ^ %1.html?q=%2 [L,QSA]

    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.html [NC,L]
</IfModule>
ThinkingMonkey
  • 12,539
  • 13
  • 57
  • 81
0

You may just add one simple RewriteRule:

RewriteRule ^/search/([a-zA-Z0-9]+)$ /search?q=$1 [QSA,NC,L]

So all in all:

<IfModule mod_rewrite.c>
   Options +FollowSymLinks
   Options +Indexes
   RewriteEngine On
   RewriteCond %{SCRIPT_FILENAME} !-d
   RewriteRule ^([^\.]+)$ $1.html [NC,L]
   RewriteRule ^/search/([a-zA-Z0-9]+)$ /search?q=$1 [QSA,NC,L]
</IfModule>

Oh by the way:

And if that's not enough:

Two hints:

If you're not in a hosted environment (= if it's your own server and you can modify the virtual hosts, not only the .htaccess files), try to use the RewriteLog directive: it helps you to track down such problems:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

My favorite tool to check for regexp:

http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

Community
  • 1
  • 1
Olivier Pons
  • 15,363
  • 26
  • 117
  • 213
  • tried the code and it doesnt auto rewrite when i submit form, and it also loads error page when i type /search/music in url bar – DJ-P.I.M.P Feb 07 '12 at 21:08
  • are you sure you restart the server each time you make a modification of the vhost? is it a `vhost` or a `.htaccess`? – Olivier Pons Feb 07 '12 at 21:54
  • yes i always reboot apache server if i modify htaccess or httpd.conf.... im not sure if i need to for htaccess but i do anywaay – DJ-P.I.M.P Feb 07 '12 at 22:02