简体   繁体   English

使用.htaccess使网址漂亮

[英]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 如果该URL包含任何文件/脚本引用,请不要重写

     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). 我已经找到(1)的解决方案,但是它们都因(2)而失败。 I am a newbie in htaccess, so need suggestions of how to do it, or a common general method to do it. 我是htaccess的新手,因此需要有关操作方法的建议或常用的通用方法。

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. 如果您想了解有关干净URL的更多信息, 本文将提供一些很好的信息。

Below are a few more resources if you want to learn more about .htaccess. 如果您想了解更多关于.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. 知道其功能后,建议您在需要时在Google上搜索所需的功能。 There are a lot of things it does that you may never need. 它可以做很多事情,您可能永远不需要。

  1. more .htaccess tips and tricks.. .htaccess的更多提示和技巧..
  2. Ultimate Htaccess File Sample 终极Htaccess文件样本
  3. URL Rewriting Guide URL重写指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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