Abhishek Bhardwaj
Abhishek Bhardwaj

Reputation: 242

Make url pretty with .htaccess

I need to adopt the following scheme:

  1. If the url contains any file/script reference, leave that without rewriting

    example.com/index.php ---> leave without rewriting url
    
  2. If the url doesn't contains any references, something similar to:

    example.com/some-thing-is-here
    

    Re-write this to:

    example.com?search.php?q=some-thing-is-here
    

I've found the solution for (1), but they all fail with (2). I am a newbie in htaccess, so need suggestions of how to do it, or a common general method to do it.

Upvotes: 0

Views: 88

Answers (1)

Jordan Mack
Jordan Mack

Reputation: 8733

The following should do what you ask. In addition to excluding files, it also excludes directories.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.example.com/search.php?q=$1 [R,L]

This article has some good information if you want to learn more about clean URLs.

Below are a few more resources if you want to learn more about .htaccess. Usage of this file is fairly complex. Once you know what it is capable of, I recommend you search on Google for the features you need, when you need them. There are a lot of things it does that you may never need.

  1. more .htaccess tips and tricks..
  2. Ultimate Htaccess File Sample
  3. URL Rewriting Guide

Upvotes: 1

Related Questions