<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[小小工程師的日常筆記]]></title> 
<link>https://xdeath.tw/index.php</link> 
<description><![CDATA[]]></description> 
<language>zh-tw</language> 
<copyright><![CDATA[小小工程師的日常筆記]]></copyright>
<item>
<link>https://xdeath.tw/read.php?52</link>
<title><![CDATA[htaccess 常用命令]]></title> 
<author>admin &lt;tszheng@twnic.net.tw&gt;</author>
<category><![CDATA[其他語言]]></category>
<pubDate>Fri, 15 Jul 2016 09:00:17 +0000</pubDate> 
<guid>https://xdeath.tw/read.php?52</guid> 
<description>
<![CDATA[ 
	本文翻譯自 : https://github.com/phanan/htaccess<br/><br/>原始翻譯 ： http://nitroxenon.com/htaccess-compilation<br/><br/>請確認你的 mod_rewrite 模組已經安裝<br/><br/><span style="font-size: 24px;"><span style="font-family: 微軟正黑體;"> 強制 WWW</span></span><br/><br/><div class="code">RewriteEngine on<br/>RewriteCond %&#123;HTTP_HOST&#125; ^example&#92;.com &#91;NC&#93;<br/>RewriteRule ^(.*)$ http://www.example.com/$1 &#91;L,R=301,NC&#93;</div><br/><br/> <span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">強制泛型 WWW</span></span><br/><br/><div class="code">RewriteCond %&#123;HTTP_HOST&#125; !^$<br/>RewriteCond %&#123;HTTP_HOST&#125; !^www&#92;. &#91;NC&#93;<br/>RewriteCond %&#123;HTTPS&#125;s ^on(s)&#124;<br/>RewriteRule ^ http%1://www.%&#123;HTTP_HOST&#125;%&#123;REQUEST_URI&#125; &#91;R=301,L&#93;</div><br/><br/> <span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">強制不使用 WWW</span></span><br/><br/><div class="code">RewriteEngine on<br/>RewriteCond %&#123;HTTP_HOST&#125; ^www&#92;.example&#92;.com &#91;NC&#93;<br/>RewriteRule ^(.*)$ http://example.com/$1 &#91;L,R=301&#93;</div><br/> <span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">強制 HTTPS</span></span><br/><br/>如果你的伺服器經過 TLS Proxy ，這個將會很實用<br/><br/><div class="code">RewriteCond %&#123;HTTP:X-Forwarded-Proto&#125; !https<br/>RewriteRule (.*) https://%&#123;HTTP_HOST&#125;%&#123;REQUEST_URI&#125;</div><br/><br/> <span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">強制 “/" 結尾</span></span><br/><br/><div class="code">RewriteCond %&#123;REQUEST_URI&#125; /+&#91;^&#92;.&#93;+$<br/>RewriteRule ^(.+&#91;^/&#93;)$ %&#123;REQUEST_URI&#125;/ &#91;R=301,L&#93;</div><br/><br/> <span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">單頁重新導向</span></span><br/><br/><div class="code">Redirect 301 /oldpage.html http://www.yoursite.com/newpage.html<br/>Redirect 301 /oldpage2.html http://www.yoursite.com/folder/</div><br/><br/> <span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">單一目錄別名 (Alias)</span></span><br/><br/><div class="code">RewriteEngine On<br/>RewriteRule ^source-directory/(.*) target-directory/$1</div><br/><br/> <span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">檔案別名 (Alias) (重新導向)</span></span><br/><br/><div class="code">RewriteEngine On<br/>RewriteRule ^$ index.fcgi/ &#91;QSA,L&#93;<br/>RewriteCond %&#123;REQUEST_FILENAME&#125; !-f<br/>RewriteCond %&#123;REQUEST_FILENAME&#125; !-d<br/>RewriteRule ^(.*)$ index.fcgi/$1 &#91;QSA,L&#93;</div><br/><br/>這個例子在幾個目錄裏面有一個 index.fcgi 檔案，如果改目錄有任何請求錯誤，將會重新導向到 index.fcgi 檔案。<br/><br/><span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">重新導向整個網站</span></span><br/><br/>Redirect 301 / http://newsite.com/<br/><br/>使用效果 :<br/><br/>www.oldsite.com/some/path/page.html<br/><br/>會變成<br/><br/>www.newsite.com/some/path/page.html<br/><br/>如果你要網站搬家，這個功能十分實用<br/><br/><span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">移除副檔名</span></span><br/><br/><div class="code">RewriteEngine On<br/>RewriteCond %&#123;SCRIPT_FILENAME&#125; !-d<br/>RewriteRule ^(&#91;^.&#93;+)$ $1.php &#91;NC,L&#93;</div><br/><br/>使用效果 :<br/><br/>example.com/users.php<br/><br/>會變成<br/><br/>example.com/user<br/><br/><span style="font-size: 24px;"><span style="font-family: 微軟正黑體;"><span style="color: #DC143C;">安全性</span></span></span><br/><br/><span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">禁止訪問</span></span><br/><br/><div class="code">Apache 2.2<br/>Deny from all<br/><br/>Apache 2.4<br/>Require all denied</div><br/><br/>請注意，使用後你將無法存取網站<br/><br/><span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">禁止訪問 (除了你)</span></span><br/><br/><div class="code">Apache 2.2<br/>Order deny,allow<br/>Deny from all<br/>Allow from xxx.xxx.xxx.xxx<br/><br/>Apache 2.4<br/>Require all denied<br/>Require ip xxx.xxx.xxx.xxx<br/>xxx.xxx.xxx.xxx 是你的 IP，能使用 IP 範圍</div><br/><br/><span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">禁止垃圾留言者訪問</span></span><br/><br/><div class="code">Apache 2.2<br/>Order deny,allow<br/>Allow from all<br/>Deny from xxx.xxx.xxx.xxx<br/>Deny from xxx.xxx.xxx.xxy<br/><br/>Apache 2.4<br/>Require all granted<br/>Require not ip xxx.xxx.xxx.xxx<br/>Require not ip xxx.xxx.xxx.xxy</div><br/><br/> <span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">禁止訪問隱藏目錄和檔案</span></span><br/><br/><div class="code">RewriteCond %&#123;SCRIPT_FILENAME&#125; -d &#91;OR&#93;<br/>RewriteCond %&#123;SCRIPT_FILENAME&#125; -f<br/>RewriteRule &quot;(^&#124;/)&#92;.&quot; - &#91;F&#93;</div><br/><br/> <span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">禁止訪問備份和原始碼目錄</span></span><br/><br/><div class="code">&lt;FilesMatch &quot;(&#92;.(bak&#124;config&#124;dist&#124;fla&#124;inc&#124;ini&#124;log&#124;psd&#124;sh&#124;sql&#124;swp)&#124;~)$&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;## Apache 2.2<br/>&nbsp;&nbsp;&nbsp;&nbsp;Order allow,deny<br/>&nbsp;&nbsp;&nbsp;&nbsp;Deny from all<br/>&nbsp;&nbsp;&nbsp;&nbsp;Satisfy All<br/> <br/>&nbsp;&nbsp;&nbsp;&nbsp;## Apache 2.4<br/>&nbsp;&nbsp;&nbsp;&nbsp;# Require all denied<br/>&lt;/FilesMatch&gt;</div><br/><br/><span style="font-size: 24px;"><span style="font-family: 微軟正黑體;"> 禁止目錄瀏覽 (Index)</span></span><br/><br/><div class="code">Options All -Indexes<br/>Options All -Indexes</div><br/><br/> <span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">禁止圖片熱鏈</span></span><br/><br/><div class="code">RewriteEngine on<br/>RewriteCond %&#123;HTTP_REFERER&#125; !^$<br/>RewriteCond %&#123;HTTP_REFERER&#125; !^http(s)?://(www&#92;.)?yourdomain.com &#91;NC&#93;<br/>RewriteRule &#92;.(jpg&#124;jpeg&#124;png&#124;gif)$ - &#91;NC,F,L&#93;</div><br/><br/> <span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">密碼保護目錄</span></span><br/><br/>首先，你需要新增一個 .htpasswd 檔案<br/><br/>htpasswd -c /home/fellowship/.htpasswd boromir<br/><br/>然後修改 .htaccess 檔案<br/><br/><div class="code">AuthType Basic<br/>AuthName &quot;One does not simply&quot;<br/>AuthUserFile /home/fellowship/.htpasswd<br/>Require valid-user<br/>AuthType Basic<br/>AuthName &quot;One does not simply&quot;<br/>AuthUserFile /home/fellowship/.htpasswd<br/>Require valid-user</div><br/><br/> <span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">密碼保護檔案</span></span><br/><br/><div class="code">AuthName &quot;One still does not simply&quot;<br/>AuthType Basic<br/>AuthUserFile /home/fellowship/.htpasswd<br/> <br/>&lt;Files &quot;one-ring.o&quot;&gt;<br/>Require valid-user<br/>&lt;/Files&gt;<br/> <br/>&lt;FilesMatch ^((one&#124;two&#124;three)-rings?&#92;.o)$&gt;<br/>Require valid-user<br/>&lt;/FilesMatch&gt;</div><br/><br/> <span style="font-size: 24px;"><span style="font-family: 微軟正黑體;"><span style="color: #DC143C;">效能</span></span></span><br/><br/><span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">壓縮文字檔案</span></span><br/><br/><div class="code">&lt;IfModule mod_deflate.c&gt;<br/> <br/>&nbsp;&nbsp;&nbsp;&nbsp;# Force compression for mangled headers.<br/>&nbsp;&nbsp;&nbsp;&nbsp;# http://developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;IfModule mod_setenvif.c&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;IfModule mod_headers.c&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SetEnvIfNoCase ^(Accept-EncodXng&#124;X-cept-Encoding&#124;X&#123;15&#125;&#124;~&#123;15&#125;&#124;-&#123;15&#125;)$ ^((gzip&#124;deflate)&#92;s*,?&#92;s*)+&#124;&#91;X~-&#93;&#123;4,13&#125;$ HAVE_Accept-Encoding<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RequestHeader append Accept-Encoding &quot;gzip,deflate&quot; env=HAVE_Accept-Encoding<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/IfModule&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/IfModule&gt;<br/> <br/>&nbsp;&nbsp;&nbsp;&nbsp;# Compress all output labeled with one of the following MIME-types<br/>&nbsp;&nbsp;&nbsp;&nbsp;# (for Apache versions below 2.3.7, you don&#039;t need to enable `mod_filter`<br/>&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;&nbsp;and can remove the `&lt;IfModule mod_filter.c&gt;` and `&lt;/IfModule&gt;` lines<br/>&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;&nbsp;as `AddOutputFilterByType` is still in the core directives).<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;IfModule mod_filter.c&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AddOutputFilterByType DEFLATE application/atom+xml &#92;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;application/javascript &#92;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;application/json &#92;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb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sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text/xml<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/IfModule&gt;<br/> <br/>&lt;/IfModule&gt;</div><br/><br/> <span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">設置頁面到期標頭</span></span><br/><br/><div class="code">&lt;IfModule mod_expires.c&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresActive on<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresDefault&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;access plus 1 month&quot;<br/> <br/>&nbsp;&nbsp;# CSS<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresByType text/css&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;access plus 1 year&quot;<br/> <br/>&nbsp;&nbsp;# Data interchange<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresByType application/json&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;access plus 0 seconds&quot;<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresByType application/xml&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;access plus 0 seconds&quot;<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresByType text/xml&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;access plus 0 seconds&quot;<br/> <br/>&nbsp;&nbsp;# Favicon (cannot be renamed!)<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresByType image/x-icon&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;access plus 1 week&quot;<br/> <br/>&nbsp;&nbsp;# HTML components (HTCs)<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresByType text/x-component&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;access plus 1 month&quot;<br/> <br/>&nbsp;&nbsp;# HTML<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresByType text/html&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;access plus 0 seconds&quot;<br/> <br/>&nbsp;&nbsp;# JavaScript<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresByType application/javascript&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;access plus 1 year&quot;<br/> <br/>&nbsp;&nbsp;# Manifest files<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresByType application/x-web-app-manifest+json&nbsp;&nbsp; &quot;access plus 0 seconds&quot;<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresByType text/cache-manifest&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;access plus 0 seconds&quot;<br/> <br/>&nbsp;&nbsp;# Media<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresByType audio/ogg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;access plus 1 month&quot;<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresByType image/gif&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;access plus 1 month&quot;<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresByType image/jpeg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;access plus 1 month&quot;<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresByType image/png&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;access plus 1 month&quot;<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresByType video/mp4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;access plus 1 month&quot;<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresByType video/ogg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;access plus 1 month&quot;<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresByType video/webm&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;access plus 1 month&quot;<br/> <br/>&nbsp;&nbsp;# Web feeds<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresByType application/atom+xml&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;access plus 1 hour&quot;<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresByType application/rss+xml&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;access plus 1 hour&quot;<br/> <br/>&nbsp;&nbsp;# Web fonts<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresByType application/font-woff&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;access plus 1 month&quot;<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresByType application/vnd.ms-fontobject&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;access plus 1 month&quot;<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresByType application/x-font-ttf&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;access plus 1 month&quot;<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresByType font/opentype&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;access plus 1 month&quot;<br/>&nbsp;&nbsp;&nbsp;&nbsp;ExpiresByType image/svg+xml&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;access plus 1 month&quot;<br/>&lt;/IfModule&gt;</div><br/><br/> <span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">關閉 eTags (禁止認證緩存)</span></span><br/><br/><div class="code">&lt;IfModule mod_headers.c&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;Header unset ETag<br/>&lt;/IfModule&gt;<br/>FileETag None</div><br/><br/> <span style="font-size: 24px;"><span style="font-family: 微軟正黑體;"><span style="color: #DC143C;">雜項</span></span></span><br/><br/><span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">設置 PHP 變數</span></span><br/><br/><div class="code">php_value &lt;key&gt; &lt;val&gt;<br/> <br/># For example:<br/>php_value upload_max_filesize 50M<br/>php_value max_execution_time 240</div><br/><br/> <span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">自定義錯誤頁面</span></span><br/><br/><div class="code">ErrorDocument 500 &quot;Houston, we have a problem.&quot;<br/>ErrorDocument 401 http://error.yourdomain.com/mordor.html<br/>ErrorDocument 404 /errors/halflife3.html</div><br/><br/> <span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">強制下載指定類型檔案</span></span><br/><br/><div class="code">//強制下載 MARKDOWN 檔案<br/>&lt;Files *.md&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;ForceType application/octet-stream<br/>&nbsp;&nbsp;&nbsp;&nbsp;Header set Content-Disposition attachment<br/>&lt;/Files&gt;</div><br/><br/> <span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">防止指定類型檔案下載</span></span><br/><br/>有時候你想在瀏覽器觀看檔案內容，但是瀏覽器強制下載，使用此方法可避免此問題。<br/><div class="code">&lt;FilesMatch &quot;&#92;.(tex&#124;log&#124;aux)$&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;Header set Content-Type text/plain<br/>&lt;/FilesMatch&gt;</div><br/><br/> <span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">允許跨站字形檔案</span></span><br/><br/><div class="code">&lt;IfModule mod_headers.c&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;FilesMatch &quot;&#92;.(eot&#124;otf&#124;ttc&#124;ttf&#124;woff)$&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Header set Access-Control-Allow-Origin &quot;*&quot;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/FilesMatch&gt;<br/>&lt;/IfModule&gt; </div><br/> <span style="font-size: 24px;"><span style="font-family: 微軟正黑體;">切換 PHP 版本</span></span><br/><br/># Alternatively, you can use AddType<br/> <br/># Alternatively, you can use AddType<br/> <br/>
]]>
</description>
</item><item>
<link>https://xdeath.tw/read.php?41</link>
<title><![CDATA[sql injection、XSS與網頁安全防護 ]]></title> 
<author>admin &lt;tszheng@twnic.net.tw&gt;</author>
<category><![CDATA[其他語言]]></category>
<pubDate>Mon, 07 Mar 2016 02:59:07 +0000</pubDate> 
<guid>https://xdeath.tw/read.php?41</guid> 
<description>
<![CDATA[ 
	寫網頁程式常常會碰到一些資安的問題，畢竟網頁是最容易遭受攻擊的服務之一<br/><br/>程式的素質往往也參差不齊，所以趁最近剛好有整理就記錄一下<br/><br/>==============XSS(跨網站指令碼)================<br/><br/>XSS主要是出現在一些輸入的資料，最終會在輸出在網頁上的情況下(留言板，布告欄，部落格等)<br/><br/>使用者輸入的內容因為會被輸出來，所以當內容中有可執行的程式碼(javascript)<br/><br/>就可以讓使用者去執行，常見的破壞方式有<br/><br/>1.導向其他帳號密碼登入的網頁騙取帳號密碼<br/><br/>2.javascript可以做的事情幾乎都有可能被插入(DDoS，alert)<br/><br/>防範方式很簡單，只要破壞程式碼的結構即可<br/><br/>PHP的話就是使用htmlspecialchars，perl則是使用encode_entities<br/><br/>將一些特殊字元做轉換<br/><code><br/>" &amp;quot<br/>' &amp;apos;<br/>< &amp;lt;<br/>> &amp;gt;<br/>& &amp;amp;<br/></code><br/>例如<script></script>&nbsp;&nbsp;就會轉換成&nbsp;&nbsp;&lt;script&gt;&lt;/script&gt;<br/><br/>瀏覽器在輸出的時候會自動轉換回去(可以看到<script></script>)，但是看原始碼是被轉換過的編碼(不可被執行)<br/><br/>==============SQL Injection(SQL資料隱碼攻擊)================<br/><br/>SQL Injection也是出現很久的攻擊方式了，主要是網頁程式輸入的參數夾帶一些SQL的命令<br/><br/>例如原本判斷帳號密碼select username from user where user = '$user' and passwd = '$passwd'<br/><br/>當使用者輸入正常的帳號密碼時不會有問題，但是如果出現 '&nbsp;&nbsp;"&nbsp;&nbsp;/*等奇怪的字元<br/><br/>最簡單就(passwd = ' and '1' = '1&nbsp;&nbsp;&nbsp;&nbsp;整行sql就變成 <br/><br/>select username from user where user = '$user' and passwd = ' ' and '1' = '1')就有可能直接跳過登入<br/><br/>SQL除了用一些特殊字元去破壞SQL的架構以外，還有很多很特殊的用法<br/><br/>例如使用union，LOAD_FILE，CHAR編碼後組成字串、url encode.....<br/><br/>基本上如果要過濾這些特殊字元是過濾不完的，而因為SQL injection會發生最根本的原因<br/><br/>其實是SQL的命令與參數值混在一起，導致輸入的值可以偽裝成SQL的一部分，所以建議使用SQL的prepare方式<br/><br/>將SQL的命令與參數分開，已php為例，上面select username就可以改成以下程式碼<br/><br/><code><br/>&nbsp;&nbsp;&nbsp;&nbsp;$db = $connect = "mysql:host=$host;dbname=$dbname;charset=utf8";<br/>&nbsp;&nbsp;&nbsp;&nbsp;$db = new PDO($connect, $user, $passwd);<br/>&nbsp;&nbsp;&nbsp;&nbsp;$sql = "select username from user where user = ? and passwd = ?";&nbsp;&nbsp;#SQL語法<br/>&nbsp;&nbsp;&nbsp;&nbsp;$sth = $db->prepare($sql);<br/>&nbsp;&nbsp;&nbsp;&nbsp;$result = $sth->execute(array($user,$passwd));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#參數值<br/></code><br/><br/>基本上就可以完全阻絕被SQL Injection了。<br/><br/>==============CSRF(跨網站指令請求)================<br/><br/>這個弱點如何被利用比較複雜，但是起因很簡單，就是當A網頁傳送資料到B網頁進行處理時<br/><br/>B網頁沒有去檢查，這些資料是不是A網頁所傳送過來的，如果是從C網頁傳送過來時<br/><br/>當C被使用iframe等方式插在網頁D中，使用者在登入網站A與該被駭網頁D，同時就會執行C<br/><br/>就有可能造成使用者不知情的情況下，去執行了C網頁的程式碼<br/><br/>那要如何確保資料是從A到B呢？<br/><br/>簡單方式就是在A網頁產生一組亂碼，存到session與form中跟著其他參數post到B<br/><br/>B只要比對session的值與傳送過來的值是否相等即可。<br/><br/>現在常見的圖形鎖也是利用相同的方式，產生一組圖與答案，存到session後到B網頁進行比對<br/><br/>
]]>
</description>
</item><item>
<link>https://xdeath.tw/read.php?28</link>
<title><![CDATA[Perl的物件導向]]></title> 
<author>admin &lt;tszheng@twnic.net.tw&gt;</author>
<category><![CDATA[其他語言]]></category>
<pubDate>Wed, 11 Mar 2015 09:03:19 +0000</pubDate> 
<guid>https://xdeath.tw/read.php?28</guid> 
<description>
<![CDATA[ 
	perl從5.X版之後也支援OO，不過他不叫object，而是以模組的形式呈現<br/><br/>對於已經習慣java或C#的使用者而言，perl的module應該不大容易上手<br/><br/>下面這段就相當於java的class程式碼<br/><br/><div class="code"><br/><br/>#!/usr/bin/perl<br/>package Mail;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //物件名稱<br/>use Mail::Sendmail;<br/>sub new &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;my ($class,$mailServer,$user,$passwd,$smtpPort) = @_;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //建構子參數，$class代表本身，所以是從mailServer開始塞入參數<br/>&nbsp;&nbsp;&nbsp;&nbsp;my %mail = (<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Smtp =&gt; $mailServer,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Port =&gt; $smtpPort,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Auth =&gt; &#123;user =&gt; $user, pass =&gt; $passwd, method =&gt; &quot;LOGIN&quot;, required =&gt; 1&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;my $self = &#123;mail =&gt; &#92;%mail&#125;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //要public的變數放在這邊，沒有放在這邊的參數就無法使用，hash的話%前面要加上反斜線<br/>&nbsp;&nbsp;&nbsp;&nbsp;bless $self, $class;<br/>&nbsp;&nbsp;&nbsp;&nbsp;return $self;<br/>&#125;<br/><br/>sub sendTo &#123;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //物件的function<br/>&nbsp;&nbsp;&nbsp;&nbsp;my ($self,$receiver,$subject,$text) = @_;&nbsp;&nbsp;//參數設定，$self表示自己本身，也就是Mail<br/>&nbsp;&nbsp;&nbsp;&nbsp;my $mail = $self-&gt;&#123;mail&#125;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //要讀取本身的public變數用$self-&gt;&#123;xxx&#125;<br/>&#125;<br/><br/></div><br/><br/>將以上這段存成Mail.pm後與主程式放在同目錄，接下來是在程式中去create Mail object<br/><br/><div class="code"><br/><br/>#!/usr/bin/perl<br/>use Mail;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//使用剛剛寫的class<br/>$mail = Mail-&gt;new(&#039;smtpserver&#039;,&#039;user&#039;,&#039;passwd&#039;,&#039;port&#039;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//建構子建立一個mail物件<br/>$mail-&gt;sendTo($receiver,$subject,$text);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //mail物件的傳送信件function<br/><br/></div><br/>
]]>
</description>
</item><item>
<link>https://xdeath.tw/read.php?24</link>
<title><![CDATA[php javascript判斷中文的方法]]></title> 
<author>admin &lt;tszheng@twnic.net.tw&gt;</author>
<category><![CDATA[其他語言]]></category>
<pubDate>Tue, 10 Feb 2015 04:06:51 +0000</pubDate> 
<guid>https://xdeath.tw/read.php?24</guid> 
<description>
<![CDATA[ 
	不管寫哪種程式，偶爾都會碰到要判斷是不是中文字<br/><br/>而基本上判斷的原理都是將文字轉成unicode<br/><br/>利用中文的unicode介於0x4E00~0x9FA5去做判斷<br/><br/>[Javascript]<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>function chineseCount(word)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #計算中文字的數量<br/>&#123;<br/>&nbsp;&nbsp;return word.split(/[&#92;u4e00-&#92;u9a05]/).length -1;<br/>&#125;<br/><br/>[PHP]<br/><br/>function chineseCount($word)<br/>&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; count( preg_split("/[&#92;x&#123;4e00&#125;-&#92;x&#123;9a05&#125;]/u",$word)<br/>&#125;<br/><br/>[Perl]<br/><br/>..........研究不出來，如果懂的人請寄信給我~__~<br/><br/>PS.以上都是在utf-8的編碼下，同時由於當初編碼的時候，繁體中文與簡體中文是混在一塊的<br/><br/>所以上面的方法會同時判斷繁簡中文，如果要分開印象中編碼的區段分成很多段<br/><br/>就要另外找一下編碼表了~~~<br/>
]]>
</description>
</item>
</channel>
</rss>