简体   繁体   English

如何将 .htaccess 规则/内容移动到虚拟主机?

[英]How to move .htaccess rules/content to virtualhost?

I am trying to move the rules/content in the .htaccess file to the virtualhost (file of my WordPress website).我正在尝试将 .htaccess 文件中的规则/内容移动到虚拟主机(我的 WordPress 网站的文件)。

Server details - Apache 2.4 on Ubuntu 20.04.3 LTS服务器详细信息- Apache 2.4 on Ubuntu 20.04.3 LTS

This is how the virtualhost file looks like after I have moved the contents of the .htaccess file -这是我移动 .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>

My Queries:-我的查询:-

1. Is it the right way to do it? 1.这是正确的做法吗? I mean, can we directly paste the contents of .htaccess file into the virtualhost file/directive inside <Location /> </Location> ?我的意思是,我们可以直接将 .htaccess 文件的内容粘贴到<Location /> </Location>内的虚拟主机文件/指令中吗?

2. <IfModule mod_rewrite.c> and <IfModule mod_mime.c> are used inside <IfModule mod_ssl.c> . 2. <IfModule mod_rewrite.c><IfModule mod_mime.c>用于<IfModule mod_ssl.c>内部。 Can we use it like this or have I made any mistakes?我们可以这样使用它还是我犯了任何错误?

The website was loading without any issues when I checked it (after restarting the Apache server) but I wanted to be sure that I am not doing anything wrong.当我检查网站时(重新启动 Apache 服务器后),网站加载时没有任何问题,但我想确保我没有做错任何事情。

Thanks in advance!提前致谢!

  1. can we directly paste the contents of .htaccess file into the virtualhost file/directive inside <Location /> </Location>我们可以直接将.htaccess文件的内容粘贴到<Location /> </Location>里面的virtualhost文件/指令中吗?

Not inside a <Location> block.不在<Location>块内。 Whilst this may "work" in this instance, it's not officially supported and other directives might break (the URL matched is the absolute file path, not the root-relative URL-path - so the first RewriteRule is not actually doing anything, since it never matches).虽然这在这种情况下可能“有效”,但它没有得到官方支持,其他指令可能会中断(匹配的 URL 是绝对文件路径,而不是相对于根的 URL 路径 - 所以第一个RewriteRule实际上没有做任何事情,因为它从不匹配)。

However, you can directly paste the contents of the .htaccess file into an appropriate <Directory> block.但是,您可以直接将.htaccess文件的内容粘贴到适当的<Directory>块中。 And disable .htaccess overrides at the same time - otherwise any .htaccess file will override the <Directory> in the server config!并同时禁用.htaccess覆盖 - 否则任何.htaccess文件都将覆盖服务器配置中的<Directory>

  1. <IfModule mod_rewrite.c> and <IfModule mod_mime.c> are used inside <IfModule mod_ssl.c> . <IfModule mod_rewrite.c><IfModule mod_mime.c>将在内部使用<IfModule mod_ssl.c> Can we use it like this我们可以这样使用吗

You can, however, there should be no need to.您可以,但是,应该没有必要。 These <IfModule> wrappers should be removed.这些<IfModule>包装器应该被移除。 This is your server, you know whether these modules are enabled or not.这是您的服务器,您知道这些模块是否已启用。

The # BEGIN / # END comment markers are only strictly relevant in .htaccess since WordPress looks for these in order to automatically update the .htaccess file. # BEGIN / # END注释标记仅在.htaccess严格相关,因为 WordPress 会查找这些标记以自动更新.htaccess文件。

For example, have it like this instead (replacing the <Location /> block):例如,改为这样(替换<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>

In your <VirtualHost> you are not actually permitting access (ie. Require all granted ) - I have added this here.在您的<VirtualHost>您实际上并未允许访问(即, Require all granted )-我已在此处添加了此内容。 But I assume this must be enabled in a parent config somewhere for this to be working?但我认为这必须在某个地方的父配置中启用才能正常工作?

Aside: I'm not sure why WordPress write the directives like this, but the RewriteBase directive is not being used here (and should be removed entirely IMO).旁白:我不知道为什么 WordPress 会写这样的指令,但是RewriteBase指令没有在这里使用(并且应该完全删除 IMO)。 The slash prefix on the last RewriteRule substitution can also be removed (this makes the code much more portable).也可以删除最后一个RewriteRule替换的斜杠前缀(这使代码更具可移植性)。

For example:例如:

# 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.) (我还在第一个RewriteRule上将.*正则表达式更改为简单的^ - 这只是一个更有效的正则表达式,因为我们不需要在此处实际匹配任何内容。)


Alternative - directly in the <VirtualHost>替代方案 - 直接在<VirtualHost>

The alternative is to move the directives directly into the <VirtualHost> container (not in a directory context).另一种方法是将指令直接移动到<VirtualHost>容器中(不在目录上下文中)。 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 .但是,这需要一些更改,因为指令处理得更早(在请求映射到文件系统之前),并且匹配(并写入)的 URL 路径现在总是相对于根目录(以斜杠开头),而不是相对的 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).在此上下文中不允许使用RewriteBase指令(并导致错误)。

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). LA-U:前缀创建一个前瞻,以确定REQUEST_FILENAME变量的最终值(否则这与REQUEST_URI服务器变量相同)。

However, strictly speaking, you should still have a <directory /var/www/example.com> container in order to allow access and disable .htaccess overrides.但是,严格来说,您仍然应该有一个<directory /var/www/example.com>容器,以便允许访问和禁用.htaccess覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM