2013-02-10 36 views
2

我試圖改變這一點:htaccess的正則表達式改寫

/news/index.php?id=101 &標題=這 - 是 - 一 - 超長標題

爲此:

/新聞/這 - 是 - 一 - 超長標題/

與htaccess的[R ewrite規則。任何想法如何?

我曾嘗試:

Options +FollowSymLinks 
RewriteEngine On 
RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteCond %{SCRIPT_FILENAME} !-f 
RewriteRule ^.+/news/index.php?title=$1 [NC,L,R] 
+0

[你嘗試過什麼?](http://www.whathaveyoutried.com/) – Nightfirecat 2013-02-10 04:54:54

+0

@Nightfirecat:!選項+的FollowSymLinks RewriteEngine敘述 在 的RewriteCond%{} SCRIPT_FILENAME -d 的RewriteCond%{} SCRIPT_FILENAME -f RewriteRule ^。+/news/index.php?title = $ 1 [NC,L,R] – user2002495 2013-02-10 04:59:14

回答

1

我覺得這是你所需要的:

Options +FollowSymLinks 
RewriteEngine On 
RewriteBase/
RewriteCond %{REQUEST_URI} !index\.php [NC] 
RewriteRule ^.*/([^/]+)/? news/index.php?id=101&title=$1 [L,NC] 

地圖默默

http://example.com/news/this-is-a-very-long-title/帶或不帶斜線

要:

http://example.com/news/index.php?id=101&title=this-is-a-very-long-title

在您的規則中,字符串id=101&未出現在替換網址中,因此如果不需要,請將其從重寫規則中刪除。

對於永久和可見重定向,請將[L,NC]替換爲[R=301,L,NC]

+0

謝謝,但我需要的其實是相反的。我想要的是映射_http://example.com/news/index.php?id = 101&title = this-is-a-very-long-title_ to _http://example.com/news/this-is- A-超長標題/ _。我會嘗試你的解決方案來做相反的事情。 – user2002495 2013-02-11 06:22:33