您的 WordPress 网站加载时间是否太长?您知道网站速度慢会影响您的SEO和用户体验吗?
除了使用插件之外,还有几种不同的方法可以加速您的网站。一种方法是利用浏览器缓存,另一种方法是压缩文件。
两者都可以通过修改和优化.htaccess文件来完成。
什么是浏览器缓存?
当有人访问您的网站时,会下载一些文件并将其存储在临时位置。
文件类型包括构成您网站的所有文档,例如HTML、图像、JavaScript和CSS文件。浏览器(Browser)缓存指的是下载文件的临时位置。
当缓存已满时,它会占用访问者计算机上的太多空间。这会导致浏览器速度变慢和网站加载时间过长。
幸运的是,有一种方法可以通过编辑 .htaccess 文件来解决这个问题。
你应该首先做什么
在开始编辑 .htaccess 文件之前,请考虑以下预防措施:
您现在可以通过 .htaccess 文件使用代码来告诉您的WordPress安装更长时间地缓存静态文件,以便返回访问您的网站的访问者。您可以使用 cPanel 或FTP。请参阅下面的说明。
一旦您有权访问您的 .htaccess 文件,然后使用其他说明进行编辑。
使用 cPanel
登录(Log)您的网站 cPanel。您通常可以通过在 Web 浏览器中输入yourdomain.com/cPanel
(Log)使用您在域上安装WordPress时提供的用户名和密码登录。登录后,查找并单击文件管理器(File Manager)。
您的 .htaccess 文件位于根文件夹中。文件名前的点表示它是 WordPress 安装中的隐藏文件。请务必选中Show Hidden Files复选框。
您应该在编辑文件之前对其进行复制,因为如果您犯了错误,您的网站可能会崩溃。要制作副本,请单击.htaccess并突出显示它。要保存副本,请单击顶部菜单上的下载按钮。
制作完副本后,您现在就可以编辑文件了。在您的 cPanel文件管理器(File Manager)中,右键单击 .htaccess 文件并单击编辑(edit)。
使用 FTP
您还可以使用FTP客户端编辑您的 .htaccess 文件。一种流行的FTP客户端是FileZilla,在下面的示例中使用。
首先(Start)打开您的FTP客户端并放入您的主机。这通常是您的域地址、用户名和密码。如果您不确定它们是什么,请联系您的虚拟主机。
使用以下步骤访问、复制和编辑您的 .htaccess 文件:
- 导航到您的根目录。
- 找到 .htaccess 文件,制作一个副本,然后将其拖到您的桌面上。
- 保留原件的副本并编辑不同的副本,以防您必须恢复原件。
- 使用(Use)记事本(Notepad)等编辑器来编辑文件。
- 完成编辑后,将文件上传到根目录。
如果您犯了错误,您的网站将无法运行。要修复它,请上传备份文件。
现在您已准备好更改您的 .htaccess 文件以加快您的WordPress网站。
使用 Gzip 压缩文件
Gzip压缩使您的文件更小,并帮助它们更快地加载。使用 gzip 将以下代码添加到您的 .htaccess 文件(在当前代码下方)将压缩CSS、HTML和PHP文件。
<IfModule mod_deflate.c>
# Compress HTML, CSS, JavaScript, Text, XML and fonts
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
# Remove browser bugs (only needed for really old browsers)
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
</IfModule>
不要忘记点击“保存更改”。
浏览器缓存
当有人访问您的网站时,他们使用的浏览器将下载与他们正在访问的页面相关的所有文件。
利用浏览器缓存意味着您只想下载必要的文件而不是不必要的资产。这些通常是页面的设计和样式,有时还包括 JavaScript 函数。
当您使用浏览器缓存时,您是在告诉您的服务器忽略以前为重复访问者下载的网页部分。
要编辑您的 .htaccess 文件,请找到它并制作两份副本,方法与使用FTP(FTP)客户端或 cPanel压缩文件时相同。
保留原始 .htaccess 文件的副本,以防更改出现任何问题。在另一个副本中进行编辑。
在 .htaccess 文件的当前内容下方添加以下代码:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A0
<FilesMatch "\.(txt|xml|js)$">
ExpiresDefault A691200
</FilesMatch>
<FilesMatch "\.(css)$">
ExpiresDefault A691200
</FilesMatch>
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac)$">
ExpiresDefault A691200
</FilesMatch>
<FilesMatch "\.(jpg|jpeg|png|gif|swf|webp)$">
ExpiresDefault A691200
</FilesMatch>
</IfModule>
<IfModule mod_headers.c>
<FilesMatch "\.(txt|xml|js)$">
Header set Cache-Control "max-age=691200"
</FilesMatch>
<FilesMatch "\.(css)$">
Header set Cache-Control "max-age=691200"
</FilesMatch>
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac)$">
Header set Cache-Control "max-age=691200"
</FilesMatch>
<FilesMatch "\.(jpg|jpeg|png|gif|swf|webp)$">
Header set Cache-Control "max-age=691200"
</FilesMatch>
</IfModule>
不要忘记单击“保存(Save)”以保留文件中的添加内容。
压缩文件并利用浏览器缓存将显着减少网页的加载时间。这将有助于您的网站在搜索中排名更高并改善用户体验。没有人愿意等待超过几秒钟来加载页面。
How To Speed Up Any WordPress Site Using .HTACCESS
Is your WordPress website taking too long to load? Do you know that a slow site wіll affect your SEO and user experience?
There are several different ways to speed up your site besides using plugins. One way is to leverage browser cache and another way is to compress your files.
Both can be accomplished by modifying and optimizing the .htaccess file.
What Is Browser Cache?
When someone visits your website, there are files that are downloaded and stored in a temporary location.
The types of files include all documents that make up your website, such as HTML, images, JavaScript, and CSS files. Browser cache refers to this temporary location for the downloaded files.
When the cache is full, it takes up too much space on the visitor’s computer. This results in a slow browser and a website that takes too long to load.
Luckily there is a way to fix this by editing your .htaccess file.
What You Should Do First
Before you start editing your .htaccess file, consider the following precautions:
- Always create a backup of your WordPress .htaccess file before changing it.
- Disable any WordPress plugins you might be using.
- Test your site speed before making changes to the .htaccess file to get a baseline measurement. You can use GTmatrix, Pingdom, or Google PageSpeed Insights.
You are now ready to use code via your .htaccess file to tell your WordPress installation to cache static files longer for return visitors to your website. You can use either cPanel or FTP. See the directions below for each.
Once you have access to your .htaccess file, then use the additional directions to make your edits.
Using cPanel
Log into your website cPanel. You can usually get there by typing yourdomain.com/cPanel into your web browser.
Log in with the username and password you were given when you installed WordPress on your domain. Once you are logged in, look for and click File Manager.
Your .htaccess file is located in the root folder. The dot before the file name indicates that it is a hidden file in your WordPress installation. Be sure to put a check in the box to Show Hidden Files.
You should make a copy of your file before editing it because if you make a mistake, you can crash your site. To make a copy, click .htaccess and highlight it. To save a copy, click the download button located on the top menu.
Once you have made your copy, you are now ready to edit the file. In your cPanel File Manager, right-click the .htaccess file and click edit.
Using FTP
You can also edit your .htaccess file using an FTP client. One popular FTP client is FileZilla, used in the example below.
Start by opening your FTP client and put in your host. This is usually your domain address, your username and password. If you are not sure what they are, contact your web host.
Use the following steps to access, copy, and edit your .htaccess file:
- Navigate to your root directory.
- Find the .htaccess file, make a copy, and drag it to your desktop.
- Keep a copy of the original and edit a different copy in case you have to restore the original.
- Use an editor such as Notepad to edit the file.
- When you have finished your editing, upload the file to your root directory.
If you have made a mistake, your website will not work. To fix it, upload the backup file.
Now you are ready to make changes to your .htaccess file to speed up your WordPress site.
Compress Your Files Using Gzip
Gzip compression makes your files smaller and helps them to load faster. Adding the code below to your .htaccess file (underneath the current code) using gzip will compress CSS, HTML, and PHP files.
<IfModule mod_deflate.c>
# Compress HTML, CSS, JavaScript, Text, XML and fonts
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
# Remove browser bugs (only needed for really old browsers)
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
</IfModule>
Don’t forget to click “Save changes.”
Browser Caching
When someone visits your website, the browser they are using will download all the files associated with the page they are accessing.
Leveraging browser cache means you want to download only the necessary files and not assets that are unnecessary. These are usually the design and style of your page and sometimes includes JavaScript functions as well.
When you use browser cache, you are telling your server to ignore parts of your webpage that were previously downloaded for repeat visitors.
To edit your .htaccess file, locate it and make two copies in the same way you did when compressing your files as outlined above using an FTP client or cPanel.
Keep a copy of the original .htaccess file in case there are any issues with the changes. Make your edits in the other copy.
Add the following code below the current content of your .htaccess file:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A0
<FilesMatch "\.(txt|xml|js)$">
ExpiresDefault A691200
</FilesMatch>
<FilesMatch "\.(css)$">
ExpiresDefault A691200
</FilesMatch>
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac)$">
ExpiresDefault A691200
</FilesMatch>
<FilesMatch "\.(jpg|jpeg|png|gif|swf|webp)$">
ExpiresDefault A691200
</FilesMatch>
</IfModule>
<IfModule mod_headers.c>
<FilesMatch "\.(txt|xml|js)$">
Header set Cache-Control "max-age=691200"
</FilesMatch>
<FilesMatch "\.(css)$">
Header set Cache-Control "max-age=691200"
</FilesMatch>
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac)$">
Header set Cache-Control "max-age=691200"
</FilesMatch>
<FilesMatch "\.(jpg|jpeg|png|gif|swf|webp)$">
Header set Cache-Control "max-age=691200"
</FilesMatch>
</IfModule>
Don’t forget to click Save to keep the additions to your file.
Compressing your files and leveraging browser cache will significantly decrease the loading time of your webpages. This will help your site rank higher in search and improve the user experience. No one wants to wait more than a few seconds for a page to load.