在本文中,我们将探讨使用 .htaccess 文件的基本原理,这对于增强 SEO 至关重要。此文件可确保搜索引擎正确索引页面。我们将学习如何管理重定向、配置安全性和提高性能。此外,我们将提供有关如何独立配置此文件的提示。
为什么需要 .htaccess
.htaccess 文件充当 Apache 服务器上 Web 开发人员的一种“远程控制”。它提供了方便的工具来调整各种参数和管理网站的行为。当无法直接访问服务器的主要配置文件时(例如,虚拟主机通常就是这种情况),.htaccess 将成为进行必要更改的有用工具。
Htaccess 通常位于您网站的根文件夹或需要特殊配置的文件夹中。例如,如果您使用 WordPress,则可以通过 Yoast SEO 插件进行配置,我们在文章中讨论过 配置 robots.txt在其他情况下,只需转到所需的文件夹并编辑现有的 .htaccess 文件即可,如果尚未创建,则创建它。
配置.htaccess 文件
301 重定向 htaccess
.htaccess 文件中的 301 重定向可比作网站上的永久重定向。它通知搜索引擎和浏览器该页面现在永久位于新地址,并建议更新书签和索引。此类重定向通常在页面被移动或删除时使用,以保持其在搜索结果中的排名并将访问者重定向到新位置。
要激活重定向,您需要在.htaccess 文件中启用相应的命令:
RewriteEngine on
此后,您可以配置最简单的重定向形式:
<IfModule mod_rewrite.c>
RewriteEngine On
Redirect 301 /old-page.html /new-page.html
</IfModule>
类似的重定向使用 永久重定向 指令如下所示:
<IfModule mod_rewrite.c>
RewriteEngine On
Redirect permanent /old-page.html http://new-domain.ru/new-page.html
</IfModule>
这些是最容易配置的重定向方法。让我们考虑使用其他更高级的重定向类型的配置,以及 mod_rewrite的 模块:
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirecting from one page to another
RewriteRule ^old-page.html$ http://www.example.com/new-page.html [R=301,L]
# Redirecting all traffic from one domain to another
RewriteCond %{HTTP_HOST} ^oldsite.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldsite.com$
RewriteRule ^(.*)$ http://www.newsite.com/$1 [R=301,L]
# Redirecting from www to non-www (or vice versa)
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
# Redirecting from HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
# Redirecting from one subdomain to another
RewriteCond %{HTTP_HOST} ^subdomain1.example.com$
RewriteRule ^(.*)$ http://subdomain2.example.com/$1 [R=301,L]
</IfModule>
302 重定向 .htaccess(表示临时重定位)很少出现。以下是此类设置的示例:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^old-page.html$ /new-page.html [R=302,L]
</IfModule>
指令错误文档
此设置允许您控制当您的网站发生 HTTP 错误时用户看到的内容。您可以显示自己的页面,而不是标准错误消息,这将为访问者提供更多信息并帮助他们了解发生了什么。只需按照以下语法即可使用:
ErrorDocument <error_code> <URL_page>
例如,
ErrorDocument 404 /errors/not_found.html
意味着当 404 发生错误,页面位于 /错误/未找到.html 将会打开。绝对 URL 的指定方式类似:
ErrorDocument 500 http://example.com/errors/server_error.html
此示例表示显示 服务器错误.html 网页,就在联盟官网 http://example.com/errors/ 如果发生 500 错误。
拒绝访问站点目录
.htaccess 文件包含以下命令 拒绝所有,这有助于拒绝访问 Web 服务器上的某些文件夹或文件。如果您想向网站访问者隐藏私人信息或重要文件以确保数据安全,这将非常有用。
要禁用列表(查看所有可用目录),您需要添加以下行:
Options -Indexes
阻止访问特定目录的示例如下:
<Directory /path_to_your_directory>
Order Deny,Allow
Deny from all
</Directory>
类似地,您可以拒绝对单个文件的访问:
<Files "file.php">
Order Deny,Allow
Deny from all
</Files>
或者按扩展名查看一系列文件:
<FilesMatch "\.(txt|log|bak)$"> # Select the desired extensions, in the example: txt, log, bak
Order Deny,Allow
Deny from all
</FilesMatch>
按 IP 阻止访问
在 .htaccess 文件中通过 IP 阻止访问是一种允许您限制特定 IP 地址或其组访问您网站的方法。如果您想阻止某些人或不受欢迎的机器人访问您的网站或其特定部分,这种方法非常有用。
使用以下命令可以阻止单个 IP 地址:
Order Deny,Allow
Allow from all
Deny from 11.22.33.44
阻止多个 IP 地址的方法类似,只需复制“拒绝从”行,其中包含所需的地址。
阻止某个范围的 IP 地址:
Order Deny,Allow
Allow from all
Deny from 11.11.11.11/24
为了仅允许特定 IP 访问,请添加代码:
Order Deny,Allow
Deny from all
Allow from 11.22.33.44
要完全拒绝访问,请使用指令 拒绝所有,对于按掩码阻止地址,请指定 从11.22拒绝.
哪里, 11.22 是 IP 地址 面膜.
根据用户代理阻止访问者
通过 .htaccess 文件中的 User-Agent 阻止用户是一种根据访问者使用的浏览器信息来控制谁可以访问您的 Web 服务器的方法。User-Agent 字符串包含有关用户用于与您的服务器交互的浏览器的数据,借助此信息,您可以限制某些用户的访问。
最常见的通过 User-Agent 进行拦截的方式是使用 mod_rewrite的:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^Bot1 [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^Bot2 [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^Bot3 [NC]
RewriteRule ^.*$ - [F,L]
</IfModule>
在这个例子中:
- RewriteCond %{HTTP_USER_AGENT} 用于检查 User-Agent 字符串。
- ^Bot1、^Bot2、^Bot3 是我们要阻止的 User-Agent 字符串的示例。NC 修饰符表示比较应不区分大小写。
- 如果满足任何 RewriteCond 条件,则 RewriteRule ^.*$ - [F,L] 适用于请求。它会发送带有 403 Forbidden 状态代码的响应 (F) 并停止处理规则 (L)。
htaccess 中的缓存
通过 .htaccess 文件配置缓存有助于加快网站加载速度,从而提高用户访问速度。其工作原理如下:某些文件(例如图像、CSS 样式和 JavaScript 脚本)在首次加载后存储在用户的浏览器缓存中。现在,浏览器可以从缓存中使用这些文件,而不必在用户每次访问页面时都从服务器重新加载它们。这减少了加载时间并提高了网站性能。
考虑一个例子:
# Enable caching for images for 1 month
<FilesMatch "\.(jpg|jpeg|png|gif|svg)$">
Header set Cache-Control "max-age=2592000, public"
</FilesMatch>
# Enable caching for CSS and JavaScript for 1 week
<FilesMatch "\.(css|js)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
另一方面,对于频繁更新的资源,有必要禁用缓存:
# Excluding caching for HTML pages and XML files
<FilesMatch "\.(html|xml)$">
Header set Cache-Control "no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "0"
</FilesMatch>
优化网站性能
此 mod_deflate 与 mod_gzip Apache 中的模块有助于压缩服务器发送到用户设备的信息。这会使文件变小,从而加快页面加载速度。但是,请务必记住,这些模块的支持和配置可能因您的服务器而异。
使用示例 mod_deflate 模块:
<IfModule mod_deflate.c>
# Compressing text file types
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE application/x-httpd-fastphp
AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>
例子 mod_gzip:
<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$ # Enabling compression for files with extensions .html, .txt, .css, .js, .php, .pl
mod_gzip_item_include mime ^application/x-javascript.* # Enabling compression for MIME types starting with application/x-javascript
mod_gzip_item_include mime ^text/.* # Enabling compression for MIME types starting with text/
mod_gzip_item_exclude mime ^image/.* # Excluding from compression MIME types starting with image/
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.* # Excluding already compressed data (responses with the Content-Encoding: gzip header)
</IfModule>
我们讨论了最常用的指令。您可以在 官方文件.
如何检查.htaccess 文件的功能
要检查您的.htaccess 文件是否在网站上运行,您可以执行以下步骤:
- 故意将 .htaccess 文件改成错误。服务器应该会给出错误响应。此方法显示文件的操作是否在服务器上正常应用。
- 检查您所做的特定更改的功能。例如,对其中一个参数进行微小调整并评估是否存在更改。
- 检查页面的状态码,看看这些变化是否与服务器的响应有关。如何做到这一点,我们之前在文章中描述过 HTTP 错误代码:服务器错误的完整列表.
- 请参阅服务器日志。它们显示与 .htaccess 文件操作相关的所有错误。
- 使用在线服务或工具进行测试。
重要的是要了解,要检查 .htaccess 文件的功能,您不需要一次执行所有步骤。从列表中选择最合适的方法并使用它就足够了。
结语
配置 .htaccess 文件是优化和保护 Apache 服务器的重要步骤。我们探索了此文件如何帮助管理重定向、压缩内容和确保安全。一旦您掌握了 .htaccess 的使用方法,您就获得了一个强大的工具来改善网站的性能和功能。