2013-09-21 44 views
2

我有一个链接象下面这样:如何在PHP中使用htaccess代码进行URL重写?

/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 

...但是当我点击该链接时,它会再次显示旧网址。

这里有什么问题?

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

+0

你的链接已经看起来像你想要做的请求 - 那么你想要做什么? – kero

+0

yes.it @ kingkero.I想使它像http://example.com/0/0/4/122,但htaccess代码不工作 – underscore

+0

htaccess代码只重写您的网址,以便请求结束在正确的地方。您使用应用程序生成的链接不受htaccess的影响。你必须自己以正确的方式格式化它。 –

回答

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] 
0

我想你是误会什么重写规则正试图在这种情况下实现的。

F.e.这个重写规则

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

将确保到http://example.com/mymethod/myarg的请求将被改写,以

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

当您生成您的应用程序中的链接,你应该知道你是怎么contstructed重写规则,你必须建立相应的链接。

-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] 
+0

PHP代码<?php echo $ sql_res ['country']?>/<?php echo $ sql_res ['state']?> .htaccess代码RewriteRule ^([^ /] *)/([^ /] *)$ /news.php?country=$1&state=$2 [L] – Vijay