Optimize WordPress with .htaccess Mastery

If you are a web developer, you must be familiar with the .htaccess WordPress file and its capabilities. In this article, we will cover the basics of .htaccess and explore the various things you can do with it to enhance your WordPress website.

Firstly, let’s understand what .htaccess is. It is a plain text file that is placed on your web server to add various controls. It is supported by many Unix/Linux based web servers, with Apache being the most popular one. You can check which server software your hosting company uses by asking their support team or by accessing a PHP file on the server.

If you are new to .htaccess, pay special attention to the name of the file. It starts with a period (.), which tells the server that it should be a hidden file. You can make changes to this file directly on the server using FTP or make changes locally and then upload it.

Before we dive into the various commands, it is essential to know what to do when things go wrong. If you mistype a command or use one that is not supported by your hosting company, you will get an “HTTP Error 500 Internal server error.” In such cases, simply back out the last change you made and upload the .htaccess file again.

Now let’s look at some of the controls or commands that you can add to your .htaccess file to enhance your WordPress website:

1. Allow larger files to be uploaded

If you want to increase the upper limit for files to be uploaded, use this command:

php_value post_max_size 20M

php_value upload_max_filesize 20M

The value at the end (20) is the size in MB. In this case, it will allow files up to 20MB to be uploaded via your WordPress media library or an upload form on the front end of your site.

2. Allow more memory for PHP files to consume

As your WordPress site grows and uses more plugins, you may encounter a common error where your site fails to load. This is because the plugin has bigger demands on the server, and PHP does not have enough memory available. To fix this, add the following line to your .htaccess file:

php_value memory_limit 128M

Be careful not to go too far with this command as you may hit the limit that your hosting company finds acceptable, resulting in an HTTP 500 error.

3. Show your PHP execution errors

Some hosting setups mute the output of PHP errors, making it hard to program changes to your site’s theme or other PHP files. You can toggle the output of PHP errors using this command:

php_value display_errors on

Remember to set it to “off” when you are done.

4. Enable browser caching

You can use .htaccess to instruct your visitor’s web browser to store/cache your media for a longer period of time. This improves performance and is commonly recommended by Google page speed. Use this command:

## EXPIRES CACHING ##

ExpiresActive On

# Good for one month

ExpiresByType image/jpeg A2592000

ExpiresByType image/gif A2592000

ExpiresByType image/png A2592000

ExpiresByType image/x-icon A2592000

ExpiresByType text/plain A2592000

ExpiresByType image/svg+xml A2592000

# Good for one week

ExpiresByType application/x-javascript M604800

ExpiresByType text/css M604800

ExpiresByType text/html M604800

ExpiresDefault A2592000

## EXPIRES CACHING ##

This command tells the browser to cache certain types of files for a specific period of time.

5. Redirect URLs

This command is handy when building a new WordPress site to replace an older one. You can redirect visitors who might have the old URL bookmarked or for pages that have been indexed in Google to the new URL in your new site. Use this command:

Redirect 301 /oldurl  http://MySite.com/newurl

The number (301) indicates whether the redirect is permanent or temporary. Stick to the correct syntax to have multiple redirects in your .htaccess file.

6. Protect your WordPress configuration file

The wp-config.php file contains important data like the database username and password. To prevent anyone from accessing it directly, use this command:

order allow,deny

deny from all

This command stops anyone from directly accessing the config file, even if there is a problem with the web server where it stops parsing PHP files correctly.

In conclusion, .htaccess is a powerful tool that can enhance your WordPress website’s performance and security. Once you have tested these commands, you can create a templated .htaccess WordPress file for future use. If you have any questions or other .htaccess tricks to share, feel free to post a comment.

Stay in Touch

spot_img

Related Articles