简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM