我有兩個網頁。我想將這兩個頁面部署在一個域上。當我呼叫根 URL 時,我想加載index.html
in 根目錄,而對于其他 URL,我想加載index1.html
in/app
目錄。
這是目錄結構。
www.example.com/index.html
www.example.com/app/index1.html
例如:請求www.example.com
加載時index.html
用于www.example.com/login
裝載/app/index1.html
用于www.example.com/signup
裝載/app/index1.html
這是我嘗試過的。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /app/index1.html [R=301,L]
</IfModule>
當我請求時,這會進行www.example.com/signup
重定向www.example.com/app/index1.html
。
但我想在app/index1.html
沒有重定向的情況下加載。請幫我。
uj5u.com熱心網友回復:
對于您顯示的示例,請嘗試遵循 .htaccess 規則。在測驗您的 URL 之前,請確保以下事項:
- 確保您的
.htaccess
檔案index.html
和您的app
檔案夾位于同一root
檔案夾中。 - 確保檔案夾中
index1.html
存在/app
檔案。 - 確保在測驗 URL 之前清除瀏覽器快取。
RewriteEngine ON
##Rule from OP's attempt to block direct access of index.html file.
RewriteRule ^index\.html$ - [NC,L]
##Rule to handle only url www.example.com here.
RewriteRule ^/?$ /index.html [QSA,NC,L]
##Rules to handle rest of the cases here..
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ /app/index1.html [QSA,NC,L]
uj5u.com熱心網友回復:
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.html$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^(.*)$ /app/index1.html [R=301,L] </IfModule>
您基本上只需要洗掉最后一個指令上的R
( redirect
) 標志。RewriteRule
但這可以優化:
DirectoryIndex index.html
RewriteEngine On
RewriteRule ^app/index1\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . app/index1.html [L]
mod_dir (ie. DirectoryIndex
)/index.html
從根服務。這可能已經在服務器配置中進行了配置,因此DirectoryIndex
此處可能不需要該指令。
第一個
RewriteRule
指令是防止不必要的檔案系統檢查的優化。這應該與被重寫的檔案匹配。IE。/app/index1.html
(不是/index.html
)。最后一個
RewriteRule
匹配單個字符(即.
- 點),因此排除了對根目錄的請求,因此可以防止每次請求根目錄時進行不必要的檔案系統檢查。^(.*)$
另一方面,正則運算式匹配所有內容,包括根目錄(目錄檢查失敗 - 第二個條件/RewriteCond
指令)。除非您使用符號鏈接,否則您可以洗掉第三個條件。
根據您的 URL 的格式,您可以使正則運算式更具限制性,并可能洗掉檢查請求未映射到檔案的第一個條件(檔案系統檢查相對昂貴)。例如。您的網址是否包含點?你給出的兩個例子沒有。點自然會分隔檔案擴展名,因此如果您的 URL 不包含點,那么它們自然不會映射到任何現有檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/482406.html