Set Up .htaccess in WordPress: A Beginner's Guide
At first glance, the .htaccess file can seem a bit intimidating, but it is actually a powerful tool that can help you improve the performance, security and SEO of your WordPress website. In this guide you learn what .htaccess is, where you can find it and how you can use it for handy settings.
What is .htaccess?
The .htaccess file (Hypertext Access) is a configuration file used by Apache web servers. It controls important things such as redirects, security and caching. In WordPress, .htaccess is mainly used to manage permalinks and redirects.
Where do you find the .htaccess file?
You can find the .htaccess file in the root folder of your WordPress installation. Here is how to get to it:
- Log in to your cPanel or use an FTP client such as FileZilla.
- Go to public_html or the folder where WordPress is installed.
- The .htaccess file may be hidden. Turn on "show hidden files" in the settings of your file manager.
- Open and edit the file with a text editor such as Notepad++ or the built-in editor in cPanel.
Handy settings for .htaccess
1. Default WordPress .htaccess code
If you ever delete your .htaccess by accident, you can recreate this basic file with the following code:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
2. SEO-friendly permalinks
WordPress uses .htaccess to generate SEO-friendly URLs. Make sure "pretty permalinks" are enabled via Settings → Permalinks.
3. 301/302 redirects (permanent/temporary redirects)
Want to redirect an old URL to a new one? Add this to .htaccess:
301 permanent redirect:
Redirect 301 /old-page https://www.yoursite.com/new-page
302 temporary redirect:
Redirect 302 /temporary-page https://www.yoursite.com/new-page
4. Basic security
Blocking IP addresses
Want to deny a specific IP address access? Add this:
Deny from 192.168.1.1
Preventing hotlinking
Prevent others from using your images directly on their site (hotlinking):
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https://(www\.)?yoursite\.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [F]
5. Simple caching optimizations
Faster loading times by enabling caching:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 week"
ExpiresByType application/javascript "access plus 1 week"
</IfModule>
Handy tips:
- Always make a backup of your .htaccess before you edit it.
- Always add code at the bottom to avoid errors.
- Test changes right away and restore if needed.
With these settings you can make your WordPress site more secure and faster.
Looking for WordPress hosting? View our plans.
0 van 0 vonden dit nuttig