問題背景:網站的歷史遺留問題,剛開始由于各種問題,一些動態頁面都是用了?id=參數的形式,對seo/seo.html" target="_blank">搜索引擎不友好,經常多方面考慮,還是改成偽靜態。這里勸各位站長在做新站時,盡量使用偽靜態,不僅對搜索引擎,對用戶體驗也友好。
問題描述:需要將 /news/show_news.php?id=1 形式的地址偽靜態成 /news/1 形式,而且 /news/show_news.php?id=1 在瀏覽器打開要 301 跳轉到 /news/1 去。
但是在 .htaccess 加入規則,出現了死循環,死循環規則如下
- RewriteRule ^(d+)$ /news/show_news.php?id=$1 [L]
- RewriteCond %{QUERY_STRING} ^id=(d+)$ [NC]
- RewriteRule ^show_news.php$ /news/%1? [L,R=301]
死循環原因:/news/show_news.php?id=1 跳轉到 /news/1,而且 /news/1 被重寫為 /news/show_news.php?id=1,/news/show_news.php?id=1 再次跳轉到 /news/1,這樣,淚流滿面!
解決辦法:只要在rewrite的時候,添加一個常量參數,這樣的url和301的url就不一樣了,規則如下:
- RewriteRule ^(d+)$ /news/show_news.php?id=$1&r=1 [L]
- RewriteCond %{QUERY_STRING} ^id=(d+)$ [NC]
- RewriteRule ^show_news.php$ /news/%1? [L,R=301]
終于解決了,淚奔……以上為真實案例,環境是apache。
新聞熱點
疑難解答