最近遇到一個比較棘手的問題,想了很久,於是突發奇想,是否有可能透過url_rewrite來解決呢?於是花了些時間去找資料跟實做,那麼把一些結果紀錄一下吧!
什麼是url_rewrite,顧名思義就是網址重寫(這樣翻對嗎?)
廢話不多範例是最好的說明,來看一下最常用的應用!
範例一:如果我想做HTTP自動轉HTTPS
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
另外紀錄一些我覺得比較特別的應用,符合某特定來源
RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^192\.168\.1\.168$ [OR] RewriteCond %{REMOTE_ADDR} ^172\.16\.$ [OR] RewriteCond %{REMOTE_ADDR} ^10\
.$ [OR]
RewriteRule .......
HTTP跟HTTPS下都能運作
<VirtualHost *:80> RewriteEngine on</VirtualHost > <VirtualHost *:443>RewriteCond ....
RewriteRule .....
RewriteEngine on</VirtualHost >RewriteCond ....
RewriteRule ....
其實我主要是要紀錄一下如何在HTTP跟HTTPS下運作,其他技術文件網路上超級多的了!
參考來源:
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
http://www.widexl.com/tutorials/mod_rewrite.html
http://www.whoopis.com/howtos/apache-rewrite.html
http://stackoverflow.com/questions/22221203/apache2-rewrite-rule-not-working-remote-address