2016-09-28 114 views
1

我試圖用.htacess重定向部分路徑。我需要重定向從.htaccess部分路徑重定向

https://example.com/blog/archive/3312 

http://blog.example.com/archive/3312 

我用下面的代碼

RewriteEngine on 
RewriteRule ^/?https://example.com/blog/(.*)$ /http://blog.example.com/$1 [L,R=301] 

嘗試,但它無法正常工作。我該如何解決這個問題?

+1

號無。沒有。在互聯網上有這樣的例子,有一個很好的文檔,這有助於。協議方案不是規則與模式匹配的主題的一部分。 _閱讀您使用的工具的文檔_。 http://httpd.apache.org/docs/current/mod/mod_rewrite.html – arkascha

回答

1

RewriteRule你只能匹配請求URI,匹配域,你需要RewriteCond

RewriteEngine on 

# comment out line below if you want to do this for both http:// and https://  
RewriteCond %{HTTPS} on 
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com)$ [NC] 
RewriteRule ^/?blog/(.*)$ http://blog.%1/$1 [L,R=301,NC,NE] 
+0

謝謝你的回答。但不幸的是,它不適合我。 –

+0

清除瀏覽器緩存後,註釋掉'RewriteCond'行並重新測試。 – anubhava

+0

仍然無效 –

1

嘗試像這樣,

RewriteEngine on 
RewriteCond %{HTTP_HOST} !^blog 
RewriteRule ^blog/(.*)$ http://blog.%{HTTP_HOST}/$1 [R=301,L] 
+0

謝謝你給出及時的答案。它正在工作,但有一個問題是它在/ blog/part中重定向。指的是http://blog.example.com/blog/archive/3312。 –

+0

請嘗試編輯! –

+0

這一次它不再重定向。 –