Complete .htaccess File Tutorial: What It Is & How to Use It

Trending 1 week ago
Source

What Is an .htaccess File?

An .htaccess record is simply a website record utilized to configure definite aspects of your site. Like redirects, customized correction pages, and more. All without having to edit nan main server configuration files.

It’s utilized connected servers that tally Apache, an open-source web server software. If your website runs connected Nginx instead, you won’t person an .htaccess file.

Here’s an illustration of what an .htaccess record mightiness look for illustration for a WordPress website: 

Where Is nan .htaccess File?

Your .htaccess record is usually located successful nan guidelines directory of your website whether you’re utilizing a content guidance system for illustration WordPress aliases you created nan tract from scratch.

One measurement to entree your site’s guidelines directory is done your web host, typically done a record head interface. Which is simply a directory wrong your web big wherever you tin entree and edit your site’s files—like your .htaccess file. 

Your web host’s server files mightiness look akin to nan record folders you person connected your computer:

But nan nonstop location of an .htaccess record depends connected its purpose.

For example, opportunity you person an .htaccess record successful nan “image” directory of your website. The directives wrong that .htaccess record would only impact files successful that directory.

But if your .htaccess record is successful nan guidelines directory of your website—the files that contains your main website files—the file’s directives would use to each nan files (and pages) connected your site. 

How to Edit Your .htaccess File

If you’re utilizing WordPress, an .htaccess record should already person been generated erstwhile you installed WordPress (we’ll spell complete what to do if this isn’t nan lawsuit successful nan adjacent section.)

And location are 3 main ways to edit it:

1. File Manager

Many web hosts connection nan expertise to entree cardinal website files done a record manager.

To do this, log successful to your host, caput to your record manager, and find your .htaccess file. 

Your .htaccess record is apt located successful your guidelines folder. The guidelines files is titled “public_html” successful our illustration below:

Double click “.htaccess” to unfastened nan plain matter editor.

Then, make immoderate changes—like adding aliases removing directives—and deed nan prevention button.

2. FTP Client

A record transportation protocol (FTP) customer is an exertion that lets you transportation files betwixt your machine and your website. Cyberduck is an illustration of an FTP client. 

You request to group up an FTP customer if you don’t person one. Which whitethorn entail reaching retired to your big for nan specifications you request to link to it.

Once you’re group up pinch an FTP client, you tin log successful to your tract via FTP. We’ll usage Cyberduck for our example.

Click “Open Connection” and participate your login details. Your web big usually provides these.

Next, alteration hidden files. Any files that statesman pinch a dot (like nan .htaccess file) are hidden by default. Meaning you can’t view—or edit—them.

To show hidden files successful Cyberduck, spell to “Edit” > “Preferences” > “Browser” and cheque nan container adjacent to “Show hidden files.”

Exit retired of nan preferences. Then, find your .htaccess file. Again, it’s apt successful your guidelines folder.

Select nan record and click “Edit.”

This opens your .htaccess record connected your machine arsenic a plain matter file. 

Make immoderate edits straight successful nan record and deed save. Your saved changes are automatically added to your .htaccess file.

3. WordPress Plugin

Plugins for illustration Yoast make it easy to edit your .htaccess record from your site’s backend.

Log successful to WordPress to download and activate nan Yoast plugin.

Then click “Yoast SEO” and prime “Tools.”

Click “File editor.”

Scroll to nan .htaccess record area and make your edits straight successful nan matter box. And prevention them by clicking “Save changes to .htaccess.”

How to Create an .htaccess File

If you don’t already person an .htaccess file, travel nan instructions beneath to create 1 yourself.

File Manager

To create an .htaccess record successful your host’s record manager, prime nan files you’d for illustration your .htaccess record to beryllium in. And support successful mind that nan .htaccess record tin impact nan directory you put it successful and its subdirectories.

Then click nan caller record button.

Next sanction your record “.htaccess” (with nan dot). Click “Confirm.”

You now person an .htaccess record fresh to edit.

FTP Client

To create an .htaccess record utilizing your FTP client, prevention a plain matter record connected your machine arsenic “.htaccess” (with nan dot). You tin create a plain matter record utilizing apps for illustration nan Notepad.

Then, wrong your chosen FTP customer (we’re utilizing Cyberduck), prime nan directory you want to spot nan .htaccess record successful (like “public_html”). And click “Upload” and prime nan .htaccess record saved to your computer.

Your .htaccess record is now fresh to use.

4 Common .htaccess Directives

These 4 .htaccess directives tin thief you amended and customize your site.

1. Add Redirects Using .htaccess

You tin redirect URLs utilizing .htaccess successful respective ways depending connected what you want to redirect.

Before adding immoderate types of redirects, you whitethorn request to load nan RewriteEngine module by adding this directive to your .htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine On
</IfModule>

Then, adhd your redirect directives nether this module. 

We’ll see nan “RewriteEngine On” codification wherever required successful each illustration beneath for clarity. But depending connected really your .htaccess record is group up, you whitethorn not request to see it each time.

Redirect Individual URLs

Redirect individual URLs pinch this directive:

Redirect 301 /old-page/ https://www.yourdomain.com/new-page/

The “old-page” should mention nan URL path—the information that comes aft your domain. This should statesman pinch a slash. 

And nan 2nd portion should beryllium nan caller page’s full URL. 

Users will beryllium automatically redirected to nan caller page whenever they effort to entree nan URL from nan aged page.

Redirect WWW URLs to Non-WWW URLs

If your domain uses nan www subdomain, you tin usage your .htaccess record to redirect it to nan non-www type pinch this directive:

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

Redirect Subfolders to New Locations

A subfolder is simply a files that exists wrong different files connected your site. For example, successful “www.yourdomain.com/blog,” nan “blog” portion is simply a subfolder.

And you tin redirect subfolders to different locations connected your domain utilizing nan .htaccess record pinch this directive (using your URL paths):

RewriteEngine On
RewriteRule ^/?blog/(.\*)$ /news/$1 [R,L]

In nan supra example, immoderate URL successful nan “blog” subfolder will beryllium redirected to nan “news” subfolder.

Redirect an Old Domain to a New Domain

You tin besides usage nan .htaccess record to redirect users from an aged domain to a caller 1 pinch this directive:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?oldsite\.com$ [NC]
RewriteRule ^(.*)$ https://newsite.com%{REQUEST_URI} [L,R=301]

This redirects some nan www and nan non-www versions of your domain to nan caller one.

Other Ways to Redirect URLs

There are different ways to redirect URLs if you don’t want to edit your .htaccess file.

First, tally an audit of your tract to spot which pages whitethorn request redirects. 

The Semrush Site Audit instrumentality tin place those pages for you.

Within nan Site Audit tool, participate your URL and click “Start Audit.”

Configure your audit settings connected nan pursuing page. Like really galore pages you’d for illustration to cheque during nan audit. And really often you want to tally nan audit. 

Click “Start Site Audit” aft configuring your audit settings.

When nan audit is done, click “Issues” and look for “# pages returned 4XX position code.” Which intends nan server couldn’t scope those pages owed to a client-side error.

Click nan “# pages” to position each affected page. 

Review which pages you request to redirect. And which ones (if any) you’d for illustration to support arsenic 404 errors. 

You mightiness so want a page to show a 404 correction if you’ve deleted nan page and location isn’t immoderate applicable page to redirect users to. 

To instrumentality redirects for these pages connected WordPress without editing your .htaccess file, you tin usage a plugin for illustration Yoast.

Click “Yoast SEO” and “Redirects” successful nan left-hand toolbar aft installing and activating nan plugin.

Select nan type of redirect you want. We'll usage 301 because we want to permanently redirect nan aged page to a caller one.

Enter nan aged URL slug—the past portion of nan URL—and nan destination URL slug. And click “Add Redirect.”

Lastly, trial nan URL by entering nan aged URL into your reside barroom to make judge it redirects to nan caller page. You whitethorn want to clear your cache first.

2. Load Custom 404 Error Pages pinch .htaccess

Your .htaccess record besides lets you load civilization 404 correction pages—the page that shows erstwhile nan server cannot find a webpage astatine that URL. This tin hap erstwhile nan page nary longer exists aliases if personification enters an incorrect URL.

You mightiness want a 404 page that reflects your brand, and possibly gives users directions to applicable contented aliases pages. 

For example, our 404 page looks for illustration this:

First, create nan page you’d for illustration to load erstwhile personification encounters a 404 error. 

Then, adhd this codification to your .htaccess file: 

ErrorDocument 404 /404-page.html

And update nan way to bespeak your civilization 404 page’s path.

Your civilization 404 page should past load immoderate clip personification visits a page connected your tract that doesn’t exist.

Other Ways to Create a Custom 404 Page

If you’re utilizing WordPress, a plugin for illustration nan Smart Custom 404 correction page tin thief you create a 404 page reasonably easily.

Here’s how:

Install and activate nan plugin erstwhile logged into your WordPress site. Then caput to “Pages” and “AddNew Page.”

Create and people your civilization 404 correction page wrong nan editor.

After publishing your page, click “Appearance” and “404 Error Page” successful nan paper bar.

Select your correction page from nan drop-down paper and click “Save Changes.”

Test your 404 page by visiting a URL that doesn’t beryllium connected your site. It should redirect to your caller 404 page.

3. Force Your Site to Load pinch HTTPS Through .htaccess

Hypertext transportation protocol unafraid (HTTPS) encrypts communications betwixt nan browser and your website. Which secures information sent from nan browser to nan server. 

Plus, HTTPS is simply a ranking signal. So, utilizing it could person a affirmative effect connected your rankings. 

If your tract has a unafraid sockets furniture (SSL) certificate, you tin unit HTTPS alternatively of HTTP utilizing your .htaccess file.

To do so, adhd these lines of codification to your .htaccess file:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Other Ways to Use HTTPS Instead of HTTP

Your big tin often thief you move to HTTPS.

Some moreover supply one-click options to unit HTTPS:

So, scope retired to your big for thief mounting up HTTPS if you don’t want to edit your .htaccess file. 

4. Enable Password Protection pinch Your .htaccess File

You tin alteration password protection connected your site—or circumstantial areas of your site—using your .htaccess file.

This whitethorn beryllium useful if you’re making tract updates and only want definite group to entree your site. Like your web designer.

You’ll request 2 files to password-protect your site: .htpasswd and .htaccess.

First, caput to your website host’s record manager.

Add a caller record extracurricular your guidelines directory. Name it “.htpasswd” and click “Confirm.”

Important: It’s champion to spot your .htpasswd record extracurricular of your guidelines directory arsenic this whitethorn beryllium much secure. 

Double click your .htpasswd record to unfastened and edit it. This is wherever you’ll adhd a username and password needed to entree your site.

You past request to encrypt your passwords (for information purposes). A free instrumentality for illustration HTPasswd Generator will encrypt your password for you.

Just participate your username, password, and click “Generate .htpasswd file.” And transcript nan output.

Paste nan output successful your .htpasswd record and prevention your file.

Lastly, unfastened your .htaccess record and adhd this directive:

# Password protection
AuthType Basic
AuthName "Restricted Area"
AuthUserFile /path/to/.htpasswd
Require valid-user

Make judge to alteration nan way of nan “AuthUserFile” statement to nan way of your .htpasswd file. And prevention nan file. 

Head to your website. Your tract should show a punctual upon loading if nan directive works. 

Enter your username and password and click “Sign in” to entree your site.

Other Ways to Password-Protect Your Site

If you’re utilizing WordPress, an easier measurement to group up password-protected pages is by utilizing nan platform’s autochthonal password protection functionality. 

Log successful to your tract and caput to nan page aliases station you want to group up password protection for.

Next to “Visibility,” click connected “Public.”

Select nan circle adjacent to “Password protected” and participate nan password you want to use. Then, exit retired of nan punctual and click “Update.”

Now erstwhile personification tries to entree nan page, they’ll spot thing that looks for illustration this:

And they’ll request nan password to entree nan content. 

But this intends you tin only person 1 password for each page—you can’t person different passwords for different users. 

For other functionality, you whitethorn want to see utilizing WordPress plugins instead.

Common Performance Issues Associated pinch .htaccess

Keep these issues successful mind if you determine to trust connected your .htaccess record to make circumstantial changes to your site.

Speed

Using galore aliases very analyzable .htaccess files tin effect your site’s performance. And origin it to tally slowly. 

Why?

Because Apache needs to publication and construe each directive each clip a petition is made to your server. 

So, cautiously see what’s basal to see successful your .htaccess and what isn’t. 

And support it arsenic concise arsenic imaginable to debar tract capacity issues. Since page speed is simply a ranking factor, you want your tract to load fast.

While there’s nary perfect magnitude for an .htaccess file, 1 measurement to find if your .htaccess record is impacting broadside velocity is to tally velocity tests earlier and aft making immoderate .htaccess record changes.

Free devices for illustration Google’s Page Speed Insights fto you trial page velocity by entering a URL:

You whitethorn request to speak pinch a developer to reappraisal your .htaccess record if your page loads slow aft making updates. 

Security

Because .htaccess files fto you redirect full sites, personification who hacks your tract tin usage nan .htaccess record to redirect your tract location else.

So, cheque your .htaccess periodically for immoderate directives you didn’t add. 

Also support regular backups of your site. Ideally, your big will besides do this for you, but you whitethorn want to support your ain backups arsenic well.

That way, you tin quickly revert your tract to an older type (which includes nan older type of your .htaccess file, too) if thing happens.

Accessibility

Improperly configured .htaccess directives tin lead to accessibility issues. Like incorrect redirects that don’t return group to nan correct page.

Users are much apt to time off your tract (and perchance caput to a competitor) if they can’t find nan contented they want.

So, it’s important to support an oculus connected immoderate method issues that mightiness originate from improperly configured .htaccess directives.

Spot Technical Issues connected Your Site

Your .htaccess record allows you to make powerful changes to your tract pinch comparative ease.

But those edits tin besides origin unwanted errors. 

Use Semrush’s Site Audit instrumentality to thief support your tract error-free erstwhile editing your .htaccess file.

Site Audit monitors errors and sends you regular updates, truthful you don’t request to interest astir keeping way of them yourself.

Try it today.

More