2

我有一个如下链接:

/country/search.php

<a href="<?php echo 'index.php?departments='.$value['department_id'].'&towns='.$value['town_id'].'&type='.$value['type_id'].'&id='.$value['id'] ?>">

当我使用下面的.htaccess代码时,它什么也不做:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /country/index.php?departments=$1&towns=$2&type=$3&id=$4 [L] 

它在工具中:

http://example.com/0/0/4/122

...但是当我单击该链接时,它再次显示旧 URL。

这里有什么问题?

我正在.htaccess使用此工具生成该代码:http ://www.generateit.net/mod-rewrite/

4

3 回答 3

2

用这个替换你的代码:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# external redirect from actual URL to pretty URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(index\.php|)\?departments=([^\s&]*)&towns=([^\s&]*)&type=([^\s&]*)&id=([^\s&]*) [NC]
RewriteRule ^ %1/%2/%3/%4/%5? [R=301,L]

# internal forward from pretty URL to actual URL
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ /country/index.php?departments=$1&towns=$2&type=$3&id=$4 [L,QSA]
于 2013-09-21T18:15:55.220 回答
0

我认为您误解了重写规则在这种情况下试图实现的目标。

Fe这个重写规则

RewriteRule ^([^/]*)/([^/]*)$ /app.php?method=$1&arg=$2

将确保对http://example.com/mymethod/myarg的请求将被重写为

http://example.com/app.php?method=mymethod&arg=myarg

当您从应用程序中生成链接时,您应该知道如何构建重写规则,并且必须相应地构建链接。

于 2013-09-21T16:38:41.193 回答
-1

原网址:

href="news.php?state="<?php echo $sql_res['state']?>"&country="<?php echo $sql_res['country']?>

理想网址:

/India/andhra%20Pradesh

和 .htaccess 代码:

RewriteEngine On 

RewriteRule ^([^/]*)/([^/]*)$ /news.php?country=$1&state=$2 [L]
于 2015-12-23T13:23:35.963 回答