URL重寫
可以通過URL重寫隱藏應用的入口文件index.php,下(xia)面是(shi)相關服務(wu)器的配置參(can)考(kao):
[ Apache ]
- httpd.conf配置文件中加載了mod_rewrite.so模塊
- AllowOverride None 將None改為 All
- 把下面的內容保存為.htaccess文件放到應用入口文件的同級目錄下
<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]
</IfModule>
[ IIS ]
如(ru)果你(ni)的服務器環(huan)境支(zhi)持ISAPI_Rewrite的話,可以配置(zhi)httpd.ini文件,添加下面的內容:
RewriteRule (.*)$ /index\.php\?s=$1 [I]
在(zai)IIS的(de)高(gao)版本下面可(ke)以配置web.Config,在(zai)中間添加rewrite節點:
<rewrite>
<rules>
<rule name="OrgPage" stopProcessing="true">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(.*)$" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php/{R:1}" />
</rule>
</rules>
</rewrite>
[ Nginx ]
在Nginx低(di)版本中,是(shi)不支持PATHINFO的,但是(shi)可(ke)以通過在Nginx.conf中配置轉發規(gui)則(ze)實(shi)現:
location / { // …..省略部分代碼
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}
}
其(qi)實內部是轉發到了ThinkPHP提供的(de)兼容URL,利用(yong)這種方(fang)式,可以解決(jue)其(qi)他不支持PATHINFO的(de)WEB服務器(qi)環境。
如果你的應用安裝在二級目錄,Nginx的偽靜態方法設置如下,其中youdomain是所在的目錄名稱。
location /youdomain/ {
if (!-e $request_filename){
rewrite ^/youdomain/(.*)$ /youdomain/index.php?s=/$1 last;
}
}
原(yuan)來的訪問URL:
//serverName/index.php/模塊(kuai)/控制器/操作/[參數名/參數值...]
設(she)置后,我(wo)們可(ke)以采用下面的方式訪問(wen):
//serverName/模塊(kuai)/控制器/操作/[參數名/參數值...]
如果你沒有修改(gai)服務器的(de)權限,可以(yi)在index.php入口文件做修改(gai),這(zhe)不是(shi)正確的(de)做法,并且不一定成功,視服務器而定,只是(shi)在框架執行前補(bu)全$_SERVER['PATH_INFO']參數
$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI' ];
文檔最后更新時間:2018-04-26 11:05:57
未解決你的問題?請到「問答社區」反饋你遇到的問題
