2013-09-21 99 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