小mod_rewrite的问题问题、mod_rewrite

2023-09-02 00:55:17 作者:毁。

我有一个分类广告网站。每个分类链接像这样原来:

I have a classifieds website. Each classified is linked like this originally:

   mydomain.com/ad.php?ad_id=Bmw_M3_M_tech_113620829

我应该使用什么重写规则,使这个链接看起来像:

What RewriteRule should I use to make this link look like:

   mydomain.com/Bmw_M3_M_tech_113620829

此外,什么我需要添加到我的.htaccess文件? 这是我到目前为止有:

Also, what do I need to add to my .htaccess file? This is what I have so far:

Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /

和我刚刚启用了这是第一次在我的Ubuntu服务器上禁用mod_rewrite的使用:

And I have just enabled mod_rewrite which was disabled at first on my Ubuntu server by using:

 a2enmod rewrite

还有什么我需要知道或做?

Anything else I need to know or do?

感谢

推荐答案

它看起来像你需要的东西是这样的:

It looks like you need something like this:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/ad.php
RewriteRule ^(.*)$ ad.php?ad_id=$1 [L]

这应该重写请求 mydomain.com/Bmw_M3 mydomain.com/ad.php?ad_id=Bmw_M3

的RewriteCond 排除被重写请求定向到 ad.php 。该重写规则只会替换代替 $ 1 。该 [L] (去年)标志停止重写的过程,以便它不适用任何更多重写对于由该规则改写的请求。

The RewriteCond excludes direct requests to ad.php from being rewritten. The RewriteRule would simply substitutes anything after mydomain.com/ in place of the $1. The [L] (last) flag stops the rewriting process so that it won't apply any more rewrites for a request that is rewritten by this rule.