亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

.htaccess 的多個重寫規則,重定向到 www,同時包含 index.php

.htaccess 的多個重寫規則,重定向到 www,同時包含 index.php

PHP
慕妹3242003 2023-09-22 16:33:31
具體來說,我想將所有非 www 頁面重定向到 www,同時還運行位于我的根目錄中的 index.php 文件。為了解決這兩個問題,我正在使用.htaccess.我已經將我的站點設置為運行 PHP 文件以在每個目錄中運行。但是當我添加從非 www 到 www 的重定向時,它就中斷了。問題似乎是多個重寫規則相互沖突。其中一個運行而另一個不運行,或者站點僅響應 500 錯誤。我的問題是,是否應該將多個重寫規則“組合”為一個?或者我只是錯誤地使用了這些多個規則?(或者這只是我搞砸的一些奇怪的語法?我已經研究這個有一段時間了哈哈)很感謝任何形式的幫助。<IfModule mod_rewrite.c>RewriteEngine OnRewriteBase /RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php  [L,QSA]# Redirect to www.RewriteCond %{HTTP_HOST} ^example.com$RewriteRule (.*) https://www.example.com/$1 [R=301,L] </IfModule>
查看完整描述

2 回答

?
青春有我

TA貢獻1784條經驗 獲得超8個贊

我發現這個有效:


RewriteEngine On

RewriteBase /

RewriteRule ^index\.php$ - [L] #if not already index.php


RewriteCond %{REQUEST_FILENAME} !-f #only if NOT a FILE (directory / non-existent file)

RewriteRule . /index.php [L] #redirect to index.php


RewriteCond %{HTTP_HOST} ^example.com$

RewriteRule ^(.*) https://www.example.com/%{REQUEST_FILENAME} [R=301,L] #redirect to https://www

</IfModule>

這不可能像所寫的那樣“工作”,因為存在許多錯誤:

  • 您缺少打開<IfModule mod_rewrite.c>指令。然而,無論如何,這個<IfModule>包裝都不是必需的,應該被刪除。

  • Apache 不支持行結束注釋。具體來說,以下行將由于“錯誤的標志分隔符”而導致 500 錯誤:

     RewriteCond %{REQUEST_FILENAME} !-f #only if NOT a FILE (directory / non-existent file)

    更新:如果您在此處沒有看到 500 錯誤響應,則您可能使用的是 LiteSpeed 服務器;而不是 Apache?在 LiteSpeed 上,此行尾注釋似乎按預期工作?。?/p>

  • www除了對目錄(包括根目錄)或真實文件(除了index.php)的請求之外,重定向到的外部重定向(最后)永遠不會被處理。在現有重寫之前,需要先進行此重定向。然而,請看下一點......

  • 您在目標 URL 中錯誤地使用了REQUEST_FILENAME(絕對文件系統路徑) - 這將導致格式錯誤的重定向。您可以使用REQUEST_URI服務器變量(完整的 URL 路徑),但請注意,您還存在雙斜杠問題。因此,它需要像下面這樣重寫:

     RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]

小要點:

  • RewriteBase這里沒有使用它,可以安全地刪除。(除非你有其他指令使用這個?)

概括

綜合以上幾點我們有:

RewriteEngine On


# Redirect to https://www

RewriteCond %{HTTP_HOST} ^example\.com$

RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


# Stop here if already index.php

RewriteRule ^index\.php$ - [L]


# Only if NOT a FILE (non-existent file)

RewriteCond %{REQUEST_FILENAME} !-f

# Rewrite to index.php (in the document root)

RewriteRule . /index.php [L]

請注意,這仍然會將目錄重寫為,與您的評論/index.php所述相反。


首先使用 302(臨時)重定向進行測試以避免潛在的緩存問題。


在測試之前您需要清除瀏覽器緩存。


查看完整回答
反對 回復 2023-09-22
?
慕容森

TA貢獻1853條經驗 獲得超18個贊

經過大量的修改/研究,我發現這個有效:


RewriteEngine On

RewriteBase /

RewriteRule ^index\.php$ - [L] #if not already index.php


RewriteCond %{REQUEST_FILENAME} !-f #only if NOT a FILE (directory / non-existent file)

RewriteRule . /index.php [L] #redirect to index.php


RewriteCond %{HTTP_HOST} ^example.com$

RewriteRule ^(.*) https://www.example.com/%{REQUEST_FILENAME} [R=301,L] #redirect to https://www

</IfModule>


<IfModule mod_headers.c>

    Header set Access-Control-Allow-Origin "*"

</IfModule>


查看完整回答
反對 回復 2023-09-22
  • 2 回答
  • 0 關注
  • 118 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號