2012-07-12 69 views
0

我想使用PHP和Apache/.htaccess進行鏈接縮短。在.htaccess文件中合併URI_REQUEST和HTTP_HOST

這裏是我的網站上鍊接的外觀:

https://mysite.com/item/this-is-a-book-on-toasters

這裏就是我想縮短鏈接來尋找上面的鏈接。

https://ms.co/Im8y2x

目前,這就是我在我的.htaccess文件:

RewriteCond %{HTTP_HOST} ^ms\.co$ [NC] 
RewriteCond %{HTTPS} =on 
RewriteRule ^(.*)$ https://mysite.com/item/$1 [L,R=301] 

所以要ms.co並重定向到mysite.com。我的問題是我在mysite.com/item上的PHP代碼不知道6位數字串(在這種情況下,Im8y2x),所以我無法查詢我的數據庫中6位數字符串的匹配URI(在本例中爲/this-is-a-book-on-toasters) 。

我知道我需要添加一些東西給我的.htaccess重寫規則我只是不確定它是什麼。可能有人能夠幫助?

回答

1

我想出了我自己的問題。如果你只留下了R=301,那麼你將保留URI(在這種情況下,Im8y2x),然後你可以用你的腳本語言(在我的情況下,PHP)來訪問它,以將其重定向到全長URI。

所以的這個代替

RewriteRule ^(.*)$ https://mysite.com/item/$1 [L,R=301] 

做到這一點:

RewriteRule ^(.*)$ https://mysite.com/item/$1 [L] 
// the only difference is the lack of ",R=301" 
0

這個怎麼樣?

RewriteCond %{HTTP_HOST} ^ms\.co/$1 [NC] 
RewriteCond %{HTTPS} =on 
RewriteRule ^(.*)$ https://mysite.com/item/$1 [L,R=301] 
+0

嗯,加入後'/ $ 1'現在不是重定向到https://mysite.com所以我覺得這是不正確的。 – 2012-07-12 20:57:53

+0

嗨@Robert,還有什麼建議嗎? – 2012-07-12 23:06:35