相信熟悉Web Server的人必定熟悉Apahce。相信熟悉Apahce的人必定知道URL Rewrite。Apache的mod_rewrite模塊,能夠幫助人們構造出各類各樣美化後的URL。在Apache中使用URL Rewrite,能夠有多種方式:一種是直接在httpd.conf中添加相應rewriterule(重寫規則),另外一種是在網站根目錄下的.htaccess中添加rewriterule(重寫規則)。可是,須要注意的是,在這兩個文件中添加到URL重寫規則略有不一樣。而這些不一樣,對於不熟悉的人來講,極可能會困擾許久。php
在Httpd.conf中:
(1)Request URI的開頭必須以斜線開始;
(2)在尋找Cache文件的時候,必須在開頭加上斜線;
(3)在使用-f或者!-f的時候,必須在開頭加上斜線。html
在.htaccess中,狀況徹底相反:
(1)Request URI的開頭不能有斜線;
(2)在尋找Cache文件的時候,不能在開頭加上斜線;
(3)在使用-f或者!-f的時候,不能在開頭加上斜線。sql
簡單而言,就是在httpd.conf中,重寫先後的URL在使用絕對路徑時須要添加斜線「/」,表示從網站根目錄開始;而在.htaccess中,則不須要。如下兩個簡單的例子:安全
httpd.conf
RewriteRule ^/$ /cache/index.html [QSA]
RewriteRule ^/([^.]+)$ /cache/$1.html [QSA]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]composer
.htaccess
RewriteRule ^$ cache/index.html [QSA]
RewriteRule ^([^.]+)$ cache/$1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]網站
或許還有更多的不一樣,你們也能夠本身關注研究一下。spa
例如 一個 寫在 .htaccess 中的..code
<IfModule mod_rewrite.c> #禁止 全部訪問.... <Files ~ "^.(htaccess|htpasswd|info.php|(\S)+.md|(\S)+.sql)$"> deny from all </Files> # 禁止訪問 vendor.... <Location /vendor> Deny from all </Location> Options +FollowSymlinks RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L] </IfModule>
而寫在 httpd.conf 中的.htm
#ThinkPhp 安全 配置... <IfModule mod_rewrite.c> #禁止 全部訪問.... <Files ~ "^.(htaccess|htpasswd|info.php|(\S)+.md|(\S)+.sql)$"> deny from all </Files> # 禁止訪問 vendor.... composer 所在目錄... <Location /vendor> Deny from all </Location> <Location /ThinkPhp> Deny from all </Location> Options +FollowSymlinks RewriteEngine On RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f RewriteRule ^/(.*)$ index.php?/$1 [QSA,PT,L] </IfModule>
在 寫的 時候....出現了 bug .......暫時這樣解決的..blog
httpd.conf
<IfModule mod_rewrite.c> #ThinkPhp 安全 配置... #禁止 全部訪問.... <Files ~ "^.(htaccess|htpasswd|info.php|(\S)+.md|(\S)+.sql)$"> deny from all </Files> # 禁止訪問 vendor.... composer 所在目錄... <Location /vendor> Deny from all </Location> <Location /Thinkphp> Deny from all </Location> </IfModule>
.htaccess
<IfModule mod_rewrite.c> Options +FollowSymlinks RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L] </IfModule>