看板 Knuckles_note
作者 標題 [Apache] Rewrite 處理網址參數
時間 2015年03月04日 Wed. PM 09:10:17
○ 使用 [QSA] 自動附加網址參數
參考 https://wiki.apache.org/httpd/RewriteFlags/QSA
QSA = Query String Append.
自動將網址上的 query string 附在 rewrite 後的網址上
例如在 rewrite rule 設定了
RewriteRule ^/product/(\d+)$ /product.php?product_id=$1 [QSA]
當使用網址 http://example.com/product/1351283?view=short
連線時,會被 rewrite 改為
http://example.com/product.php?product_id=1351283&view=short
後面的 ?view=short 自動被改為 &view=short 了
○ 修改網址參數
參考 https://simonecarletti.com/blog/2009/01/apache-rewriterule-and-query-string/
例如要將 page.php?id=123 改成 123.html
若使用 Rewrite ^page\.php\?id=(\d+) $1
是無效的,抓不到網址參數 id=123
要使用
RewriteCond %{QUERY_STRING} ^id=(\d+)$
RewriteRule ^page\.php$ %1.html?
先由 RewriteCond 將網址參數的 123 存至 %1
再用 RewriteRule 將 page.php 改成 %1.html
後面要再加個 ?,才會去除原本的 ?id=123
參考
https://medium.com/浦島太郎的水族缸/htaccess-with-rewrite-3dba066aff11
--
※ 作者: Knuckles 時間: 2015-03-04 21:10:17
※ 編輯: Knuckles 時間: 2023-03-16 00:57:08 (台灣)
※ 看板: KnucklesNote 文章推薦值: 1 目前人氣: 0 累積人氣: 315
回列表(←)
分享