2012-03-30 75 views
0

我想使用.htaccess永久重定向我所有的後續鏈接;.htaccess永久重定向和規範化

  • mysite.com/post/44/my-post-title.html
  • mysite.com/post/44/
  • mysite.com/44/

to;

  • mysite.com/my-post-title.html

注:如果鏈接不包含我-後title.html,我使用PHP腳本來得到這個職位標題來自帖子id ie- 44並將其重定向到標準鏈接模式(mysite.com/my-post-title.html)。

回答

1

您可以使用RewriteEngine來實現。

您需要添加以下行.htaccess文件:

RewriteEngine On 

RewriteRule TheTule RedirectionURL [R] 

凡TheRule是一個正則表達式滿足在這種情況下,你的病情,你可以這樣寫:

^/後/(0- 9)*/$

和RedirectionURL可以是:我-後title.html

它看起來像:

RewriteRule ^\/post\/(0-9)*\/$ my-post-title.html [R] 

您將不得不編寫自己的規則。 不要忘記[R]它用於外部HTTP重定向。

+0

爲什麼搜索引擎將索引所有可能的網址mysite.com/post/44,mysite.com/44,mysite.com/post/44/my-post-title.html,以及我的site.com/my-post -title.html – 2012-03-31 08:40:29

+0

如果您在談論Google,那麼向他們提供您的網站的站點地圖,以便僅索引這些鏈接。您可以使用Google的網站管理員工具執行此操作。動態生成該站點地圖。 – vedarthk 2012-03-31 17:11:45