2012-07-13 51 views
0

我有我的原始動態網站的靜態副本。原來的網站有網址是這樣的:如何交換「index.php?」通過httaccess在URL字符串「index.php @」mod_rewrite

http://www.mysite.com/index.php?module=prodreviews&func=showcontent&id=728

然而,因爲現在我有我的網站的靜態副本,所有的URL都@代替?和.htm擴展名。

所以當前的URL格式:http://www.mysite.com/[email protected]=prodreviews&func=showcontent&id=728.htm

什麼是mod_rewrite規則將重定向爲舊的URL結構的任何請求(中網址,沒有擴展名爲.htm「?」),以新的網址格式?我基本上希望能夠通過舊鏈接訪問我網站的任何人都將被帶到正確的頁面,即使URL格式已經改變。

我一直在玩寫一些規則,但沒有骰子。

這是我到目前爲止,但這只是在任何URL上自動添加.htm擴展名。我仍然需要替換任何具有「?」的URL請求之後的「PHP」與「@」:

Options +FollowSymlinks Options +Indexes RewriteEngine On RewriteBase/
# add .html file extension 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.htm -f 
RewriteRule ^(.+)$ $1.htm [L] 

回答

0

您需要包括查詢字符串作爲你的重寫規則的目標的一部分:

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}@%{QUERY_STRING}\.htm -f 
RewriteCond %{QUERY_STRING} ^(.*)$ 
RewriteRule ^(.*)$ /[email protected]%1.htm? [L] 
+0

偉大的,正是我一直在尋找的 - 完美的作品。 – user1522622 2012-07-13 10:36:06

+0

一直在測試它 - 它確實適用於大多數情況。但有一個補充:對於某些查詢,我需要忽略似乎在一定程度上混亂鏈接結構的大寫字母。例如,如果這是某人按照我的網頁上的網站鏈接的網址鏈接,我怎麼會忽略查詢中的大寫字母?所以這個http://www.mytestcom/modules.php?op = modload&name = Sections&File = index&req = listarticles&secid = 4將需要成爲http://www.mytest.com/[email protected]=modload&name=sections&file=index&req = listarticles&secid = 4.htm – user1522622 2012-07-13 11:03:49

+0

爲了轉換爲小寫字母,您需要訪問服務器的配置或vhost配置並使用'RewriteMap',請參閱:http://www.chrisabernethy.com/force-lower-case -urls與 - mod_rewrite的/ – 2012-07-13 15:54:13