我的目標是這樣的:
我們的網站位于 webroot 中,即/web.
它包含.htaccess檔案,但我們希望提供內容,/web/content但我們不希望用戶看到的 URL/content僅包含他們請求的初始路徑。
例子:
用戶向 URL 發出請求:
example.com/color/cool/blue
此請求轉至:
/webroot/color/cool/blue (which does not exist)
內容在
/webroot/content/color/cool/blue/index.htm
我們希望用戶example.com/color/cool/blue在瀏覽器中看到,但從example.com/content/color/cool/blue/index.htm??.
我們還希望直接訪問一些目錄,例如:
example.com/exeption/foo.pdf
我們這樣做是為了將動態站點轉換為靜態站點,因此簡單地將所有內容移至根目錄或切換 webroot 都不是選項。
uj5u.com熱心網友回復:
假設:
- 目錄檔案路徑不包含點。
在根.htaccess檔案中嘗試以下操作:
# Disable directory listings (mod_autoindex) since "DirectorySlash Off"
Options -Indexes -MultiViews
# Prevent trailing slash being appended to directories
DirectorySlash Off
# File to serve from requested directory
DirectoryIndex index.htm
RewriteEngine On
# Remove trailing slash on any URL that is requested directly (excludes rewritten URLs)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*)/$ /$1 [R=301,L]
# Rewrite root
RewriteRule ^$ content/ [L]
# If request maps to a directory in "/content" then rewrite and append trailing slash
RewriteCond %{DOCUMENT_ROOT}/content/$1 -d
RewriteRule ^([^.] )$ content/$1/ [L]
我們還希望直接訪問一些目錄,例如:
example/exeption/foo.pdf
您不一定需要在這方面添加任何內容。盡管我假設您的意思是“檔案”,而不是“目錄”。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/465400.html
上一篇:使用htaccess從一個URL重定向到另一個URL
下一篇:GolangAPI的HTTPS
