2016-07-12 130 views
4

我想通過htaccess使用mod重寫PHP網站。使用Mod Rewrite來更改URL結構

URL結構如下:

example.com/ex.php?ez=DF004AE 

它必須成爲:

example.com/ex/DF004AE.htm 

什麼是正確的腳本添加到我的.htaccess才能做到這一點?

+0

你嘗試過什麼嗎? – apokryfos

+0

新的網址是否存在? – starkeen

+0

不,它只是一個漂亮的SEO鏈接 – marya

回答

2

使用此:

RewriteEngine On 
RewriteRule ^ex/([^/]*)\.htm$ /ex.php?ez=$1 [L] 

它會給你以下網址:

example.com/ex/DF004AE.htm

如果你的意思是它是html的(不熱媒)只需添加l在重寫規則。

+0

再次,我認爲問題是詢問相反的,即從查詢字符串URL到相應的一個。 – apokryfos

+1

準確地說,我是如何讀它的@apokryfos! OP已要求URL example.com/ex.php?ez=DF004AE'成爲'example.com/ex/DF004AE.htm'。 – Lag

1
RewriteEngine On 
RewriteRule ^ex/([^/]*)\.html$ /ex.php?ez=$1 [R=301,NE,L] 

如果您不希望地址欄反映此更改,請刪除R = 301。 NE參數用於確保請求不會被轉義。

2

您可以使用以下規則:

RewriteEngine on 
# Redirect from "/ex.php?ez=foobar" to "/ex/foobar.htm" 
RewriteCond %{THE_REQUEST} /ex\.php\?ez=([^\s]+) [NC] 
RewriteRule^/ex/%1.htm? [L,R] 
#################### 
# internally map "/ex/foobar.htm" to "/ex.php?ez=foobar" 
RewriteRule ^ex/([^.]+)\.htm$ /ex.php?ez=$1 [NC,L]  

這將/ex.php?ez=foobar轉化爲/ex/foobar.htm