WordPressoffers several advantages to non-technical users who want to create and manage a website. One of the greatest advantages is how much you can do without touching the core files of your WordPress website.

Want toadd Amazon affiliate links to your posts?Remove the sidebar on your site?Display your most recent posts? You can accomplish these tasks — and many more — without leaving your dashboard.

Grow Your Business With HubSpot's Tools for WordPress Websites

But there are some tasks, like setting up 301 redirects or forcing HTTPS on your site, that require you to edit a file in the WordPress core. This file is known as .htaccess and it can enable you to configure specific settings on your site.

In this post, we’re going to cover everything you need to know about .htaccess, including:

If you'd rather follow along with a video, check out this walkthrough fromFix Runner:

Let’s get started with a brief overview of what .htaccess is and why it matters in WordPress.

More specifically, the .htaccess file name provides commands for controlling and configuring theApache web server在你的网站托管。这个服务器主机价值上千万eds or thousands of other sites as well on what are termed “server farms.” These farms are set up and run by web hosting providers. Without .htaccess, every site owner on the same Apache web server would have to use the same settings for their site. Thankfully, almost every hosting provider allows .htaccess.

With this file, you can set up redirects, force SSL,block IP addresses, deny access to sensitive files, send custom HTTP responses to certain requests,prevent hotlinking, and configure other settings required for your unique site.

Below, we’ll cover where this powerful file is located, how to create and edit it, and more.

WordPresshtaccess location

The WordPress .htaccess file is located in the root directory of your WordPress site. Depending on your hosting provider, the root directory may be a folder labelled public_html, www, htdocs, or httpdocs. You can locate it by using File Manager in your hosting account’s cpanel. Let’s walk through the process step-by-step.

Log in to your hosting account’s control panel.

  1. Open theFile Manager.
  2. In the navigation menu on the lefthand side of your screen, click on thepublic_htmlfolder.
  3. Open the folder labeled “wordpress.”
  4. Look for the .htaccess file.
  5. If you don’t see the folder, then go toSettings.Click Settings button in cPanel to show hidden default htaccess file in WordPress

    Image Source

  6. A window labeled “Preferences” should appear.
  7. Select the box labeled “Show Hidden Files.”

    Image Source

You should now be able to see the .htaccess file.

If you still can’t see the .htaccess file, then it’s possible it doesn’t exist. Don’t worry — you can create one in a few easy steps. Let’s walk through the process in the next section.

WordPress默认htaccess

WordPressshould automatically create an .htaccess file for you — but sometimes it is unable to because of an issue with file permissions. In that case, follow the steps below.

    1. Log into your WordPress dashboard and go toSettings>Permalinks.
      Click Settings > Permalinks in WordPress dashboard to make WordPress create default htaccess file
    2. Without changing anything, scroll to the bottom and clickSave Changes.
    3. WordPresswill now try to generate an .htaccess file. If it can’t, you’ll see an error message saying “.htaccess file is not writeable” at the bottom of the page.
    4. You will need to manually create the .htaccess file. Start by logging in to your hosting account’s control panel.
    5. Open theFile Manager.
      Click File Manager icon in cpanel to begin process of creating default wordpress htaccess file

      Image Source

    6. In the navigation menu on the lefthand side of your screen, click on thepublic_htmlfolder.
    7. Click the +File icon in the toolbar at the top of your screen.
      Click +File icon in cpanel to create default wordpress htaccess file

      Image Source

    8. Type in “.htaccess” into theNew File Nameinput field.
    9. Click创建新文件.
    10. Right-click the newly created file to edit it.
    11. Add the following code.

      # 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
    12. Save and close the file.

Now that you know how to create a default .htaccess file for your WordPress site if it doesn’t exist already, you’re ready to edit it. Let’s look at how below.

Edit htaccess WordPress

Editing the .htaccess file — or any core WordPress file — is risky. You could end up deleting code you shouldn’t, adding incorrect code, or making another mistake that breaks your site.

To mitigate the risks, you should take at least one of the precautionary steps below before making direct edits to the htaccess file.

  • Backup your WordPress siteso you can restore an earlier version if you make a mistake.
  • Use a staging siteto test your edits before pushing them live on your public-facing site.
  • Create a backup .htaccess file and download it to your computer. That way, if your edits in the default .htaccess file cause any problems, you can upload the backup file.

Once you’ve completed at least one of the steps above, you’re ready to edit. There is more than one way to edit the .htaccess file in WordPress. Let’s go over how to do so manually and using aWordPressplugin.

Edit htaccess WordPress Using cPanel

If you’d rather notadd another plugin to your WordPress site, then you can use cPanel in your hosting account. If you opt for this method, you’ll have to complete at least one of the precautionary steps outlined above yourself.

Here are the steps for editing the .htaccess file in WordPress using cPanel.

  1. Log in to your hosting account’s control panel.
  2. Open theFile Manager.
  3. In the navigation menu on the lefthand side of your screen, click on thepublic_htmlfolder.
  4. Open the folder labeled “wordpress.”
    Click folder labelled WordPress in cPanel to find and edit htaccess

    Image Source

  5. Find the.htaccessfile and right-click to edit it.
  6. Add any code before the line that reads # BEGIN WordPress.

Edit htaccess WordPress Using a Plugin

If you’d like to automate some of the prep work before editing your .htaccess file, you can use a plugin likeHtaccess File Editor. Htaccess File Editor allows you to test edits before saving and automatically backup and restore the default version of your htaccess file. That makes it amust-have WordPress pluginfor beginners trying to edit this special configuration file.

Below is the process for editing htaccess in WordPress with the Htaccess File Editor plugin.

  1. Log into your WordPress dashboard.
  2. Install and activate the Htaccess File Editor plugin.
  3. Go toSettings>WP Htaccess Editor.
  4. Create a new line before # BEGIN WordPress.
    Create new line before # BEGIN WordPress to begin editing htaccess file with plugin
  5. You can now add any relevant code snippets to make your own rules.

Below we’ll walk through two common examples of what you can do with the htaccess file: set up redirects and force HTTPS.

WordPresshtaccess Redirect

Setting up redirects — specifically,301 redirects— on your WordPress site can prevent your visitors from seeing a 404 error page instead of the content they requested. It also tells

search engines that a post or page has permanently moved so they know to find, crawl, and rank the new page in approximately the same position as the old page on SERPs.

Let’s say you decide to consolidate duplicate content on your site. In that case, you can set up redirects from outdated posts to single, updated pages. Or let’s say you rename the URL of a single post or page. You can set up a redirect so that any internal or external links on your site with the old URL will send visitors to the new URL. Or let’s say you change your domain name. In that case, you can set up redirects so that any visitors trying to visit the old domain name will be sent to the new one.

These are just a few reasons you might want to set up redirects using the .htaccess file in WordPress. Now let’s look at how.

Redirecting a Single Post or Page

To redirect a single post or page, add the following line of code for each post or page you want to redirect.


Redirect 301 /old-url-slug https://yourdomain.com/new-url-slug

Notice you only have to include theWordPressslugof the old URL (ie. the part after your domain name). You should include the full URL of the new post or page, however.

Let’s look at a specific example. Say have two duplicate blog posts aboutWordPressappointment plugins. Then you can combine the content of both posts and redirect the URL with less traffic and backlinks to the URL with more traffic and backlinks. So let’s say you’d like to redirect “//www.eigoj.com/website/best-appointment-booking-plugins-for-wordpress”到“//www.eigoj.com/marketing/wordpress-appointment-plugin”. Then, you would add the following line of code in the .htaccess file:


Redirect 301 /best-appointment-booking-plugins-for-wordpress //www.eigoj.com/marketing/wordpress-appointment-plugin


Anyone who clicks on an internal or external link with the old URL will now be redirected to the new URL.

Redirecting Your Entire Site

To redirect an entire website to a new domain, add the following code snippet to the .htaccess file in WordPress:


RewriteEngine上
RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301,NC]


Replace the placeholder text in the second, third, and fourth lines with your actual old and new domain names. This code snippet will preserve your link structure. So, for example, your contact page which was “www.olddomain.com/contact” will redirect to “www.newdomain.com/contact”.

Force HTTPS htaccess WordPress

Forcing HTTPS is a necessary step if you’ve recentlyinstalled an SSL certificate on your WordPress site.

AnSSL certificateis a standard security technology for encrypting information between a visitor’s browser and your website. Because it helps keep sensitive information like passwords and payment information safe, visitors feel safer on sites that are encrypted with SSL. It also may help you rank better. Back in 2014, Google announced that it wouldgive sites with SSL a minor ranking boost.

If you’ve recently installed an SSL certificate, then you need to take additional steps to configure your site so that it uses the secure URL with HTTPS instead of HTTP. According to theWordPressCodex, you can do so by adding the following code to the .htaccess file.


SSLOptions +StrictRequire
SSLRequireSSL
SSLRequire %{HTTP_HOST} eq "www.WordPress.com"
ErrorDocument 403 https://www.WordPress.com

This will ensure any visitors using the HTTP version of your site address will be redirected to the version withSSL.

For a more in-depth look at how to force HTTPS on your WordPress site using the .htaccess file or a plugin, check outHow to Force HTTPS on Your WordPress Site.

Leveraging the Power of the WordPress htaccess

The .htaccess file can offer you more sophisticated control over yourWordPresswebsite. You can set up redirects to the most up-to-date pages on your site, force SSL to ensure visitors are being sent to the HTTPS version of your site, and configure other settings tosecure your WordPress site.

Use HubSpot tools on your WordPress website and connect the two platforms  without dealing with code. Click here to learn more.

Use HubSpot tools on your WordPress website and connect the two platforms  without dealing with code. Click here to learn more.

Originally published Mar 22, 2021 7:00:00 AM, updated September 30 2021

Topics:

WordPressWebsite