我有关于mod_rewrite的查询

首先,我仍然是mod_rewrite的初学者。
大家如果我有这个.htaccess代码

RewriteEngine On RewriteRule ^index/(.*)/$ index.php?usr=$1 [L] 

必须改变链接(href)的forms

 <a href="index/image/">Click Here</a> 

或者按原样离开

 <a href="index.php?usr=5">Click Here</a> 

如果将其保留为<a href="index.php?usr=5">Click Here</a>将以这种方式显示链接http://domain.com/index/image/

编辑#1

这里是一个html代码的例子。

 <div id="main_menu"> <ul> <li><a href="index.php">{$lang.Nav_Home}</a></li> <li><a href="sections.php?cid={$value.cat_id}">{$value.cat_name}</a></li> {else} <li id="moreSection_{$secNum}" style="display: none;"><a href="sections.php?cid={$value.cat_id}">{$value.cat_name}</a></li> <li><a href="javascript:;" onclick="showHideMoreSec('moreSection',this)"> <img src="images/arrow_down.png" alt="{$lang.MainMenu_MoreSection}" title="{$lang.MainMenu_MoreSection}" /> </a></li> </ul> </div> 

我认为你的问题是你必须处理PATHQUERY_STRING作为2个单独的元素。

 http://some.server/sections.php?cid=5 ^^^^^^^^^^^^ ^^^^^ 

|

| path query string

尝试一下这样的规则:

 RewriteEngine on RewriteCond %{QUERY_STRING} ^$ [OR] RewriteCond %{QUERY_STRING} ^cid=(.*)$ [NC] RewriteRule ^/sections.php$ /sections/%1? [NC,L,R=301] 

这些规则正在做以下。 如果没有QUERY_STRING ,则第一个RewriteCond会跳过。 第二个RewriteCond将等号后面的位保存在variables%1 。 最后一行RewriteRule会在遇到一个/sections.php行时生成新的PATH/ sections /%1 ,并且有一个QUERY_STRINGcid = …。

一个示例html文件:

 <html> <body> <a href="/sections.php?cid=5">Click Here</a> </body> </html> 

hover在上面的点击这里链接显示这个:

 http://localhost/sections.php?cid=5 

点击这个链接把我带到这个URL:

 http://localhost/sections/5 

我的/ var / www / html有以下内容:

 % tree /var/www/html /var/www/html |-- test.html `-- sections `-- 5 1 directory, 2 files 

资源

  • Apache维基 – 操纵查询string
  • statichtml blog – 基于查询string参数的mod_rewrite

不。只有在用户向服务器发送请求时, RewriteRule才有意义。 它不会影响他们将鼠标hover在链接上时看到的内容,也不会改变他们您所改变的地址到您所改变的地址(也就是说,您正在改写index/image/ to index.php?usr=image ;它不会改变它,从index.phpindex/image/ )。