.htaccess https、www 和子域靜默重寫 (.htaccess https, www and subdomain silent rewrite)


問題描述

.htaccess https、www 和子域靜默重寫 (.htaccess https, www and subdomain silent rewrite)

我需要:

  • 添加 www,所以用戶將:

    • 輸入 https://example.com/test .html
    • 在瀏覽器中查看 https://www.example.com/test.html
    • example 獲取文件.com/test.html
  • 添加http‑> https 重定向,因此用戶將:

    • 輸入 http://www.example.com/test.html
    • 在瀏覽器中查看 https://www.example.com/test.html
    • example.com/test.html
    • 獲取文件
  • 添加子域 silent 重定向,因此用戶將:<


    參考解法

    方法 1:

    You may try these rules in site root .htaccess:

    DirectoryIndex index.php
    RewriteEngine On
    
    # add www for main domain
    RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
    
    # add https for all domains
    RewriteCond %{HTTPS} !on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
    
    # for new domain silently forward to new/ directory
    RewriteCond %{HTTP_HOST} ^new\.example\.com [NC]
    RewriteRule !^new/ new%{REQUEST_URI} [NC,L]
    

    Make sure you don't have any other code except these lines in site root and you must clear browser cache completely.

    方法 2:

    With your shown attempts/samples, could you please try following. Please make sure to you your htaccess rules in following way and clear your browser cache before testing your URLs.

    RewriteEngine ON
    RewriteCond %{HTTPS} !on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
    
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
    
    RewriteCond %{HTTP_HOST} ^new\.example\.com [NC]
    RewriteCond %{REQUEST_FILENAME} !‑f
    RewriteCond %{REQUEST_FILENAME} !‑d
    RewriteRule ^(.*)/?$ new/index.php?$1 [QSA,L]
    
    RewriteCond %{HTTP_HOST} ^new\.example\.com [NC]
    RewriteCond %{REQUEST_URI} !^/new [NC]
    RewriteRule ^(.*)/?$ new/$1 [L]
    
    RewriteCond %{REQUEST_FILENAME} !‑f
    RewriteCond %{REQUEST_FILENAME} !‑d
    RewriteRule ^(.*)/?$ index.php?$1 [QSA,L]
    

    (by ForeenanubhavaRavinderSingh13)

    參考文件

    1. .htaccess https, www and subdomain silent rewrite (CC BY‑SA 2.5/3.0/4.0)

#url #.htaccess #apache






相關問題

正則表達式查找和替換 url 不起作用或如何使其起作用 (Regex to Find and Replace url does not work or how to make it work)

子網站的 URL 重寫? (URL Rewriting for a Subsite?)

使用 htaccess 從基本 URL 中刪除變量 (Remove variable from base URL with htaccess)

在 URL 地址中使用項目名稱而不是 ID (Using item's name in the URL address instead of IDs)

Perl Chèn chuỗi vào url tại các địa điểm cụ thể (Perl Inserting string into a url at specific places)

Cách ẩn mọi thứ sau tên trang web của bạn bằng .htaccess (How to hide everything after your webpage name with .htaccess)

在 gwt 框架中使用參數更改 url (Changing url with parameters in gwt framework)

用於 SEO 目的的 Nodejs URL 修改 (Nodejs URL modifications for SEO purposes)

將用戶重定向到外部站點 (Redirecting user to external site)

Python 循環遍歷 csv 文件中的 url 返回 \ufeffhttps:// (Python Looping through urls in csv file returns \ufeffhttps://)

主頁中的 React Router 目標 div id (React Router target div id in home page)

.htaccess https、www 和子域靜默重寫 (.htaccess https, www and subdomain silent rewrite)







留言討論