【小(xiǎo)編推薦】ThinkPHP​¥δ3.2——URL重寫

2014-06-26   |&nαε<bsp;  發布者:梁國(guó)芳 &nσ∞bsp; |   查看(kγ≤¶àn):3320次

Thinkphp

可(kě)以通(tōng)過URL重寫隐藏應用(yòn  ♣↔g)的(de)入口文(wén)件(jiàn)index.php,下(xià&∞β¶)面是(shì)相(xiàng)關服務器(qì)的(de)配置參考✔×☆:

[ Apache ]

  1. httpd.conf配置文(wén)件(j®©iàn)中加載了(le)mod_rewr →$"ite.so模塊
  2. AllowOverride None 将None改為(wèi)§® All
  3. 把下(xià)面的(de)內(nèi)容保存為(wèi≥☆).htaccess文(wén)件(jiàn)放(fàng)到(dào)應用☆ε÷(yòng)入口文(wén)件(jiàn)§ 的(de)同級目錄下(xià)
  1. <IfModule mod_rewrite.c>
  2. RewriteEngine on
  3. RewriteCond %{REQUEST_FI¶×δ"LENAME} !-d
  4. RewriteCond %{REQUEST_FILENAME} !-♥×<∑f
  5. RewriteRule ^(.*)$ index.php/₩€$1 [QSA,PT,L]
  6. </IfModule>

[ IIS ]

如(rú)果你(nǐ)的(de)服務器(qì)環境支♠α"持ISAPI_Rewrite的(de)話(huà),可(✘≤$kě)以配置httpd.ini文(wén)×<←件(jiàn),添加下(xià)面的(de)內(nèi)容:

  1. RewriteRule (.*)$ /index\.php\?s=$1 [I]

在IIS的(de)高(gāo)版本下(xià✘₽)面可(kě)以配置web.Config,在中間(jiān)添加rewrit♣¥e節點:

  1. <rewrite>
  2. <rules>
  3. <rule name="OrgPage" stopProcessing="true">
  4. <match url="^(.*)$" />
  5. <conditions logicalGrouping="MatchAll">
  6. <add input="{HTTP_HOST}" pattern="^(.*)$" />
  7. <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
  8. <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
  9. </conditions>
  10. <action type="Rewrite" url="index.php/{R:1}₹Ω" />
  11. </rule>
  12. </rules>
  13. </rewrite>

[ Nginx ]

在Nginx低(dī)版本中,是(shì)不(bù)支持PATHINFO的(≤♥↔de),但(dàn)是(shì)可(kě)以通(tōng)過在Nginx.coβ€$>nf中配置轉發規則實現(xiàn):

  1. location / { // …..省略部分(fēn)代碼
  2. if (!-e $request_filename) {
  3. rewrite ^(.*)$ /index.php?s=$1 last;
  4. break;
  5. }
  6. }

其實內(nèi)部是(shì)轉發到(dào)了(le)≠"ThinkPHP提供的(de)兼容模式的(de)URL,利用(yò₩§σ¶ng)這(zhè)種方式,可(kě)以解決其他(tā)不​​≥ε(bù)支持PATHINFO的(de)WEB服務器(qì)環境。

如(rú)果你(nǐ)的(de)ThinkPHP安裝在二級目錄,Nginx的$™↓ (de)僞靜(jìng)态方法設置如(rú§Ωσ)下(xià),其中youdomain是(shì→£✘♠)所在的(de)目錄名稱。

  1. location /youdomain/ {
  2. if (!-e $request_filename){
  3. rewrite ^/youdomain/(.*)$ /youdomain/index.php?s=$1 last;
  4. }
  5. }

原來(lái)的(de)訪問(wèn)URL:

http://serverName/index.php/模塊/控制(zhì)♣¶器(qì)/操作(zuò)/[參數(shù)↔∑名/參數(shù)值...]

設置後,我們可(kě)以采用(yòng)下(xià)面₽€₩₩的(de)方式訪問(wèn):

http://serverName/模塊/控制(zhì)器(qì)/操作(zu< ò)/[參數(shù)名/參數(shù)值..↕​₩₹.]

默認情況下(xià),URL地(dì)址中的Ω >(de)模塊不(bù)能(néng)省略,>§如(rú)果你(nǐ)需要(yào)簡化("πhuà)某個(gè)模塊的(de)URL訪問(wèn)地(dì)址,可₹© (kě)以通(tōng)過設置模塊列表和(hé)默認模塊€λ₽≈或者采用(yòng)子(zǐ)域名部署到(dào)模塊的(de)方式> ®解決,請(qǐng)參考後面的(de)模塊和(hé)域名部署®↑σ部分(fēn)。