我有一個網站https://example.com
其中有 2 個子目錄https://example.com/admin和https://example.com/store.
- 我想
/store從 URL 中隱藏,所以我的請求看起來像:https://example.com/shop.php?id=someid - 但是,我也想
https://example.com/admin/index.php作業。 - 我找到了一些同時實作 1 和 2 的答案,但它們更改了基本根目錄,因此我的所有 css、js、影像都不會加載
到目前為止,我有:
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteRule ^store/(.*) /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteRule !^store/ store%{REQUEST_URI} [L]
這實作了 1 但不是 2。
uj5u.com熱心網友回復:
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ RewriteRule ^store/(.*) /$1 [L,R=301] RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ RewriteRule !^store/ store%{REQUEST_URI} [L]
在第二條規則中...排除模式/admin中的子目錄(以及/store)。并添加一個條件以排除包含檔案擴展名(即、、等)的請求。例如:RewriteRule .css.js.png
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteCond %{REQUEST_URI} !\.\w{2,4}$
RewriteRule !^(store|admin)($|/) store%{REQUEST_URI} [L]
或者(盡管效率稍低),排除任何已經映射到物理檔案的請求:
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^(store|admin)($|/) store%{REQUEST_URI} [L]
此規則假定您.htaccess在子目錄中有另一個檔案/store也使用重寫引擎。(但如果是這樣的話,那么第一條規則實際上并沒有做任何事情。)
除非您托管多個域/站點,否則您不需要檢查請求的第一個條件Host。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/433999.html
