首頁  >  問答  >  主體

javascript - htaccess rewrite 的問題

我有個thinkphp開發的網站,企用了mod rewrite

##

<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine On
  
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f

  RewriteRule ^(.*)$ index.php/ [QSA,PT,L]

</IfModule>

thinkphp 和程式運作正常,現在我的需求是,我在網站根目錄的hot目錄下,放了一個vue開發的小程序,並開啟了histroy路由模式,

按照開發文件說明為了讓所有的路徑都可以被伺服器識別,也要透過RewriteRule 把所有的存取Rewrite到index.hml上

也就是說要把http://wwww.xxx.com/hot/item/1000 這樣的url 解析到/hot/index.html
上,
其它還是用thinkphp來控制,

試過了好多設定都不行,本人對.htaccess檔指令不熟悉,所以希望大家給個方案


回覆一樓的內容,我也貼一下這裡vue 運行也是正常的,在開啟histroy 模式下,你進入http://www.xxx.com/hot可以正常運行,點擊各個連結也是可以正常跳轉,因為html5的histroy模式只是在瀏覽器段改變了url地址欄,並沒有向server請求數據,但是當用戶直接用http://www .xxx.com/hot/item/1000

這樣的url來訪問的時個,由於server上並沒有這個文件所以不會正常返回,但是通過apache的htaac​​cess文件可以把用戶訪問不存在的文件跳轉到指定的檔案上,就如我上面貼的thinkphp下的設定檔就是起這個作用,我現在的要求是把在/hot目錄下的請求不要轉發給thinkphp來執行,而是轉發個/hot/ index.html來處理。

我嘗試如下

<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine On
  
  RewriteCond %{REQUEST_URI} ^/(hot|hot/.*)$
  RewriteRule ^/hot/index\.html$ - [L]

  RewriteRule ^(hot|hot/.*)$ hot/index.html$ [L]

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  #RewriteRule ^hot/(.*)$ hot/index.html$
  #RewriteRule ".?" "-" [S=1]
  #RewriteRule ^/(hot|hot/.*)$ /hot/index.html$ [L]
  RewriteRule ^(.*)$ index.php/ [QSA,PT,L]

</IfModule>

但是不起作用,不知道為什麼? ###
phpcn_u1582phpcn_u15822712 天前421

全部回覆(2)我來回復

  • 为情所困

    为情所困2017-05-16 16:59:11

    對 thinkphp 不了解,可以看一下 thinkphp 的路由配置可否指定某一個目錄不同的路由方案

    如果不行可能要用 nginx 了

    回覆
    0
  • PHP中文网

    PHP中文网2017-05-16 16:59:11

    解決了,原來的我想法是對的,一開始老是被解析到主目錄的index.php原來是我的引用資源路徑的問題
    貼出有效 配置

    <IfModule mod_rewrite.c>
      Options +FollowSymlinks
      RewriteEngine On
      
      RewriteCond %{REQUEST_URI} ^/(hot|hot/.*)$
      RewriteRule ^/hot/index\.html$ - [L,NC]
      
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^(hot|hot/.*)$ hot/index.html [L]
    
      RewriteCond %{REQUEST_URI} !^/(hot|hot/.*)$
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^(.*)$ index.php/ [QSA,PT,L]
    
    </IfModule>

    測試位址: http://www.wx2share.com/hot/
    如果不做url rewrite 直接透過 以上網址進入,一切功能也是正常的,

    但是如果透過以下網址進入
    http://www.wx2share.com/hot/i...
    就會回傳 404,但是做了 url rewrite 就可以正常訪問了

    回覆
    0
  • 取消回覆