您需要使用mod_rewrite和條件(RewriteCond
),它與QUERY_STRING
服務器變量匹配。
請試試以下根目錄.htaccess
文件以上任何現有的mod_rewrite指令。
RewriteEngine On
# PART 1 : Redirect old category URLs
RewriteCond %{QUERY_STRING} ^cat=(\w+)
RewriteRule ^portfolio/category$ /projecten/%1? [R=302,L]
# PART 2 : Redirect other old URLs
RewriteCond %{QUERY_STRING} ^s=(pagename-\w{3}-\w{3})
RewriteRule ^portfolio/post\.php$ /projecten/%1? [R=302,L]
這假定在pagename-xxx-xxx
的xxx
是3個文字字字符(即a-z
,A-Z
,0-9
或_
)。
更新#1:上RewriteRule
取代的後?
是必要的,以便從目標移除查詢字符串。或者,使用Apache 2.4+上的QSD
標誌。
當您確定工作正常時,將302
(臨時)重定向到301
(永久)。瀏覽器緩存301重定向,這可能會使測試出現問題。
更新#2:隨着 「PART 2」 關於更新網址,儘量不要使用以下:
# PART 2 : Redirect other old URLs
# To essentially remove the date prefix, eg. "YYYY-MM-DD-"
RewriteCond %{QUERY_STRING} ^s=/d{4}-/d{2}-/d{2}-(.+)
RewriteRule ^portfolio/post\.php$ /projecten/%1? [R=302,L]
^s=[0-9{4}]+-(.+?)/?[0-9{2}]+-(.+?)/?[0-9{2}]+-(.+?)/?(.*)$
這是一個有點的搗爛,但看起來過於複雜,你試圖實現什麼?例如,爲什麼你需要匹配可選的斜線(即/?
)?你的示例URL不包含任何斜線?
諸如[0-9{4}]+
之類的正則表達式模式並沒有達到您認爲正在執行的目的。這將匹配任何字符{}
1次或更多次。你似乎試圖做的是完全匹配4位數字。例如。 [0-9]{4}
(與使用速記字符類的/d{4}
相同)。
你到目前爲止嘗試了什麼? – darrylyeo