我使用 cPanel 托管https://files.example.com,我想https://files.example.com/anything-here重定向到我的主網站并轉發路徑,所以你最終會在https://www.example.com/anything-here. 這可能嗎?
請注意,我的.htaccess檔案中還有一個強制 SSL 重定向。
https://www.example.com 是一個 Google 網站。
我的.htaccess檔案:
ErrorDocument 400 /index.html
ErrorDocument 401 /index.html
ErrorDocument 403 /index.html
ErrorDocument 404 /index.html
ErrorDocument 500 /index.html
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Header always set Content-Security-Policy "upgrade-insecure-requests;"
uj5u.com熱心網友回復:
https://www.example.com是一個 Google 網站。
如果這兩個站點位于不同的服務器上,并且您只需要將所有內容從一臺主機重定向到另一臺主機并保持相同的 URL 路徑,那么除了以下 mod_alias指令外,您的.htaccess檔案中似乎不需要任何files.example.com內容Redirect:
# Redirect everything to https://www.example.com/ and maintain URL-path
Redirect 302 / https://www.example.com/
該Redirect指令是前綴匹配,匹配后的所有內容都復制到目標 URL 的末尾。因此,請求/foo被重定向到https://www.example.com/foo.
但是,如果您有其他主機名指向您的 cPanel 帳戶,那么您需要使用 mod_rewrite 規則并檢查請求的主機名。
例如,在現有.htaccess檔案的末尾:
# Redirect every request for "files.example.com"
# to https://www.example.com/ and maintain URL-path
RewriteCond %{HTTP_HOST} ^files\.example\.com [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=302,L]
參考:
- https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect
- https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule
更新:
但我剛剛意識到它也在轉發我的主機上確實存在的檔案的路徑。我只希望它將無效路徑轉發到www.example.com。
在這種情況下,您需要這樣做:
RewriteCond %{HTTP_HOST} ^files\.example\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=302,L]
第二個和第三個條件在發出重定向之前檢查請求未映射到現有檔案(或目錄)。
洗掉檢查HTTP_HOST是否不需要的第一個條件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/336348.html
標籤:.htaccess
上一篇:如何在Angular中獲取輸入值(element.setAttribute("[(ngModel)]","Title"不起作用)
下一篇:為什么strlen()算錯了?
