我正在嘗試將 .htaccess 檔案中的規則/內容移動到虛擬主機(我的 WordPress 網站的檔案)。
服務器詳情-Apache 2.4 on Ubuntu 20.04.3 LTS
這是我移動 .htaccess 檔案的內容后虛擬主機檔案的樣子 -
<IfModule mod_ssl.c>
<VirtualHost *:443>
Protocols h2 http/1.1
ServerName example.com
ServerAlias www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com$1 [L,R=permanent,NC]
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
<Location />
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# BEGIN Imagify: webp file type
<IfModule mod_mime.c>
AddType image/webp .webp
</IfModule>
# END Imagify: webp file type
</Location>
</VirtualHost>
</IfModule>
我的查詢:-
1.這是正確的做法嗎?我的意思是,我們可以直接將 .htaccess 檔案的內容粘貼到里面的 virtualhost 檔案/指令中<Location /> </Location>嗎?
2. <IfModule mod_rewrite.c>并且<IfModule mod_mime.c> 在里面使用<IfModule mod_ssl.c>。我們可以這樣使用它還是我犯了任何錯誤?
當我檢查網站時(重新啟動 Apache 服務器后),網站加載時沒有任何問題,但我想確保我沒有做錯任何事情。
提前致謝!
uj5u.com熱心網友回復:
- 可以直接把.htaccess檔案的內容粘貼到virtualhost file/directive里面嗎
<Location /> </Location>
不在<Location>塊內。雖然這在這種情況下可能“有效”,但它沒有得到官方支持,其他指令可能會中斷(匹配的 URL 是絕對檔案路徑,而不是相對于根的 URL 路徑 - 所以第一個RewriteRule實際上沒有做任何事情,因為它從來沒有火柴)。
但是,您可以直接將.htaccess檔案內容粘貼到適當的<Directory>塊中。并同時禁用.htaccess覆寫 - 否則任何.htaccess檔案都將覆寫<Directory>服務器配置中的 !
<IfModule mod_rewrite.c>并且<IfModule mod_mime.c>在里面使用<IfModule mod_ssl.c>。我們可以這樣使用嗎
您可以,但是,應該沒有必要。這些<IfModule>包裝應該被移除。這是您的服務器,您知道這些模塊是否已啟用。
該# BEGIN/# END注釋標記只在嚴格相關的.htaccess,因為WordPress的尋找這些以自動更新.htaccess檔案。
例如,改為這樣(替換<Location />塊):
<Directory /var/www/example.com>
Require all granted
# Disable .htaccess overrides
AllowOverride None
# BEGIN WordPress
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
# BEGIN Imagify: webp file type
AddType image/webp .webp
# END Imagify: webp file type
</Directory>
在您中,<VirtualHost>您實際上并未允許訪問(即。Require all granted) - 我已在此處添加了此內容。但我認為這必須在某個地方的父配置中啟用才能正常作業?
旁白:我不知道為什么 WordPress 會寫這樣的指令,但是RewriteBase這里沒有使用該指令(應該完全洗掉 IMO)。也可以洗掉最后一個RewriteRule 替換的斜杠前綴(這使代碼更具可移植性)。
例如:
# BEGIN WordPress
RewriteEngine On
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
# END WordPress
(I also change the .* regex to simply ^ on the first RewriteRule - this is just a more efficient regex, since we don't need to actually match anything here.)
Alternative - directly in the <VirtualHost>
The alternative is to move the directives directly into the <VirtualHost> container (not in a directory context). However, this requires some changes since the directives are processed much earlier (before the request is mapped to the filesystem) and the URL-paths matched (and written to) are now always root-relative (starting with a slash), instead of being relative. There is also no "looping" by the rewrite engine, unless this is explicitly triggered. The RewriteBase directive is not permitted in this context (and results in an error).
For example:
# BEGIN WordPress
RewriteEngine On
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{LA-U:REQUEST_FILENAME} !-f
RewriteCond %{LA-U:REQUEST_FILENAME} !-d
RewriteRule ^/. /index.php [L]
# END WordPress
# BEGIN Imagify: webp file type
AddType image/webp .webp
The LA-U: prefix creates a look-ahead in order to determine the final value of the REQUEST_FILENAME variable (otherwise this is the same as the REQUEST_URI server variable).
However, strictly speaking, you should still have a <directory /var/www/example.com> container in order to allow access and disable .htaccess overrides.
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/335957.html
