{"id":37762,"date":"2022-10-28T07:00:37","date_gmt":"2022-10-28T14:00:37","guid":{"rendered":"https:\/\/dhblog.dream.press\/blog\/?p=37762"},"modified":"2025-05-21T18:01:16","modified_gmt":"2025-05-22T01:01:16","slug":"guide-to-wp-and-htaccess","status":"publish","type":"post","link":"https:\/\/www-dev.dreamhost.com\/blog\/guide-to-wp-and-htaccess\/","title":{"rendered":"Beginner\u2019s Guide to the WordPress .htaccess File"},"content":{"rendered":"\n<p>Keeping your site safe should be a top priority for every administrator. WordPress is <a href=\"https:\/\/wordpress.org\/about\/security\/\" target=\"_blank\" rel=\"noopener\">a secure platform<\/a> out of the box, but that doesn\u2019t mean it\u2019s impervious to attacks. Fortunately, even if you aren\u2019t a security expert, you can use a file called <i>.htaccess<\/i> to harden your site\u2019s security policies.<\/p>\n\n\n\n<p><a href=\"https:\/\/help.dreamhost.com\/hc\/en-us\/articles\/216456227\" target=\"_blank\" rel=\"noopener\"><i>.htaccess<\/i> is a configuration file<\/a> for the Apache web server, which serves many WordPress sites. It\u2019s a powerful tool that helps safeguard your site and boost its performance through some minor tweaks to its code. By editing this file, you can ban users, create redirects, prevent attacks, and even deny access to specific parts of your site.<\/p>\n\n\n\n<h2 id=\"h-an-introduction-to-the-htaccess-file\" class=\"wp-block-heading\"><b>An Introduction to the .htaccess File<\/b><\/h2>\n\n\n\n<p><i>.htaccess<\/i> is short for \u201cHyperText Access.\u201d It\u2019s a configuration file that determines how <a href=\"https:\/\/help.dreamhost.com\/hc\/en-us\/articles\/217214317\" target=\"_blank\" rel=\"noopener\">Apache-based servers<\/a> interact with your site. In simpler terms, <i>.htaccess<\/i> controls how files in a directory can be accessed. You can think of it as a guard for your site because it decides who to let in and what they\u2019re allowed to do.<\/p>\n\n\n\n<p>By default, an <i>.htaccess<\/i> file is typically <a href=\"https:\/\/wordpress.org\/support\/article\/htaccess\/\" target=\"_blank\" rel=\"noopener\">included in your WordPress installation<\/a>. The main purpose of this file is to improve security and performance. Plus, it also enables you to override your web server\u2019s settings.<\/p>\n\n\n\n<p>You\u2019ll most likely find your <i>.htaccess<\/i> file in your site\u2019s root directory. Since <i>.htaccess<\/i> applies to both its own directory and any subdirectories within that main folder, it impacts your entire WordPress site.<\/p>\n\n\n\n<p>It\u2019s also worth noting that the <i>.htaccess<\/i> file does not have a file extension. The period at the start simply makes sure the file remains hidden.<\/p>\n\n\n\n<h2 id=\"h-how-to-edit-your-wordpress-htaccess-file\" class=\"wp-block-heading\"><b>How to Edit Your WordPress .htaccess File<\/b><\/h2>\n\n\n\n<p>Editing the <i>.htaccess<\/i> file is, in practice, as simple as editing any other text file. However, because this is a core file, making changes to it can have unintended consequences.<\/p>\n\n\n\n<p>For this reason, it\u2019s vitally important that you <a href=\"https:\/\/www.dreamhost.com\/blog\/how-to-back-up-wordpress-tutorial\/\" target=\"_blank\" rel=\"noopener\">back up your site<\/a> before you begin, regardless of whether you\u2019re a beginner or an experienced developer.<\/p>\n\n\n\n<p>When you\u2019re ready to edit your <i>.htaccess<\/i> file, you can access it using <a href=\"https:\/\/www.dreamhost.com\/blog\/ftp-sftp-guide-for-wordpress-users\/\" target=\"_blank\" rel=\"noopener\">Secure File Transfer Protocol (SFTP)<\/a> or Secure Shell (<a href=\"https:\/\/help.dreamhost.com\/hc\/en-us\/articles\/115006413028\" target=\"_blank\" rel=\"noopener\">SSH<\/a>). You will find <i>.htaccess<\/i> in your site\u2019s root directory:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"861\" height=\"588\" data-src=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-htaccess-file-via-ftp.jpg\" alt=\"WordPress .htaccess file\" class=\"wp-image-37765 lazyload\" data-srcset=\"https:\/\/www-dev.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-htaccess-file-via-ftp.jpg.webp 861w, https:\/\/www-dev.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-htaccess-file-via-ftp-300x205.jpg 300w, https:\/\/www-dev.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-htaccess-file-via-ftp-768x524.jpg 768w, https:\/\/www-dev.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-htaccess-file-via-ftp-600x410.jpg.webp 600w, https:\/\/www-dev.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-htaccess-file-via-ftp-750x512.jpg.webp 750w, https:\/\/www-dev.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-htaccess-file-via-ftp-100x68.jpg 100w\" data-sizes=\"(max-width: 861px) 100vw, 861px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 861px; --smush-placeholder-aspect-ratio: 861\/588;\" \/><\/figure><\/div>\n\n\n<p>Open the file using your preferred text editor, such as TextEdit or Notepad. If the file hasn\u2019t been edited before, you\u2019ll see the following default information:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"1024\" height=\"413\" data-src=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-htaccess-file-1024x413.jpg\" alt=\"WordPress .htaccess file\" class=\"wp-image-37766 lazyload\" data-srcset=\"https:\/\/www-dev.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-htaccess-file-1024x413.jpg 1024w, https:\/\/www-dev.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-htaccess-file-300x121.jpg 300w, https:\/\/www-dev.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-htaccess-file-768x310.jpg 768w, https:\/\/www-dev.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-htaccess-file-1536x620.jpg 1536w, https:\/\/www-dev.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-htaccess-file-600x242.jpg.webp 600w, https:\/\/www-dev.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-htaccess-file-750x303.jpg.webp 750w, https:\/\/www-dev.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-htaccess-file-100x40.jpg 100w, https:\/\/www-dev.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-htaccess-file.jpg.webp 1552w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/413;\" \/><\/figure><\/div>\n\n\n<p>It\u2019s important not to add or change anything between the # BEGIN and # END tags. Instead, all new code should be added after this block.<\/p>\n\n\n\n<p>At this point, all you need to do is add your code and save the file. When you\u2019re including multiple new functions, it\u2019s best to save and test each one separately. If an error occurs, this will make it much easier to troubleshoot which change caused the problem.<\/p>\n\n\n\n<p>While almost all WordPress installations will already contain an <i>.htaccess<\/i> file, in some cases, you may need to create one. You can do this using a text editor of your choice, as long as you save it with the right file name: <i>.htaccess<\/i> with no extension.<\/p>\n\n\n\n<p>It\u2019s also important to configure <a href=\"https:\/\/wordpress.org\/support\/article\/changing-file-permissions\/\" target=\"_blank\" rel=\"noopener\">the file\u2019s permission settings<\/a> correctly. You can then upload <i>.htaccess<\/i> to your site\u2019s root directory.<\/p>\n\n\n\n<h2 id=\"h-9-things-you-can-do-with-your-wordpress-htaccess-file\" class=\"wp-block-heading\"><b>9 Things You Can Do With Your WordPress .htaccess File<\/b><\/h2>\n\n\n\n<p>Now that you\u2019re familiar with the <i>.htaccess<\/i> file, it\u2019s time to get up close and personal. We\u2019re going to introduce a number of ways you can easily boost your site\u2019s security and performance by editing this file.<\/p>\n\n\n\n<p>Simply use the code snippets we\u2019ve provided below, and remember to create a backup before you start!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-deny-access-to-parts-of-your-site\"><b>1. Deny Access to Parts of Your Site<\/b><\/h3>\n\n\n\n<p>One of the most useful things you can do with <i>.htaccess<\/i> is <a href=\"https:\/\/help.dreamhost.com\/hc\/en-us\/articles\/216363167\" target=\"_blank\" rel=\"noopener\">deny access<\/a> to certain pages and files. There are a few files you should consider hiding in this way for security reasons, such as <a href=\"https:\/\/wordpress.org\/support\/article\/editing-wp-config-php\/\" target=\"_blank\" rel=\"noopener\">your wp-config.php file<\/a>.<\/p>\n\n\n\n<p>You can do this by adding the following code, which will cause a <a href=\"https:\/\/www.dreamhost.com\/blog\/how-to-fix-wordpress-404-error\/\" target=\"_blank\" rel=\"noopener\">404 error<\/a> to appear if anybody attempts to view the file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;Files ~ \"\/wp-config.php\"&gt;\nOrder Allow,Deny\nDeny from All\n&lt;\/Files&gt;<\/pre>\n\n\n\n<p>In cases where sensitive data should be hidden, it can be useful to restrict access to the corresponding directory. Since many WordPress sites use the same folder structure, this setup can leave your site vulnerable. If you add the following line, it will disable the default directory listing functionality:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Options -Indexes<\/pre>\n\n\n\n<p>This will stop users and robots from viewing your folder structure. If anybody tries to access it, they\u2019ll be shown a <a href=\"https:\/\/www.dreamhost.com\/blog\/how-to-fix-403-forbidden-error-wordpress\/\" target=\"_blank\" rel=\"noopener\">403 error<\/a> page instead.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-2-redirect-and-rewrite-urls\"><b>2. Redirect and Rewrite URLs<\/b><\/h3>\n\n\n\n<p><a href=\"https:\/\/www.dreamhost.com\/blog\/guide-to-redirects\/\" target=\"_blank\" rel=\"noopener\">Creating redirects<\/a> enables you to automatically send users to a specific page. Redirects can be particularly useful if a page has moved or been deleted, and you want users who attempt to access that page to be taken somewhere else.<\/p>\n\n\n\n<p>You can accomplish this with a plugin such as <a href=\"https:\/\/wordpress.org\/plugins\/redirection\/\" target=\"_blank\" rel=\"noopener\">Redirection<\/a>, but it\u2019s also possible to do it by editing the <i>.htaccess<\/i> file. To create a redirect, use the following code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Redirect \/oldfile.html http:\/\/www.example.com\/newfile.html<\/pre>\n\n\n\n<p>You can probably see what\u2019s going on here. The first part is the path to the old file, while the second part is the URL you want visitors to be redirected to.<\/p>\n\n\n\n<div class=\"article-newsletter article-newsletter--gradient\">\n\n\n<h2>Get Content Delivered Straight to Your Inbox<\/h2><p>Subscribe now to receive all the latest updates, delivered directly to your inbox.<\/p><form class=\"nwsl-form\" id=\"newsletter_block_\" novalidate><div class=\"messages\"><\/div><div class=\"form-group\"><label for=\"input_newsletter_block_\"><input type=\"email\"name=\"email\"id=\"input_newsletter_block_\"placeholder=\"Enter your email address\"novalidatedisabled=\"disabled\"\/><\/label><button type=\"submit\"class=\"btn btn--brand\"disabled=\"disabled\"><span>Sign Me Up!<\/span><svg width=\"21\" height=\"14\" viewBox=\"0 0 21 14\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n<path d=\"M13.8523 0.42524L12.9323 1.34521C12.7095 1.56801 12.7132 1.9304 12.9404 2.14865L16.7241 5.7823H0.5625C0.251859 5.7823 0 6.03416 0 6.3448V7.6573C0 7.96794 0.251859 8.2198 0.5625 8.2198H16.7241L12.9405 11.8535C12.7132 12.0717 12.7095 12.4341 12.9323 12.6569L13.8523 13.5769C14.072 13.7965 14.4281 13.7965 14.6478 13.5769L20.8259 7.39879C21.0456 7.17913 21.0456 6.82298 20.8259 6.60327L14.6477 0.42524C14.4281 0.205584 14.0719 0.205584 13.8523 0.42524Z\" fill=\"white\"\/>\n<\/svg>\n<\/button><\/div><\/form><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-3-force-your-site-to-load-securely-with-https\"><b>3. Force Your Site to Load Securely With HTTPS<\/b><\/h3>\n\n\n\n<p>&lt;style&gt;.embed-container { position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; } .embed-container iframe, .embed-container object, .embed-container embed { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }&lt;\/style&gt;&lt;div class=&#8217;embed-container&#8217;&gt;&lt;iframe src=&#8217;https:\/\/www.youtube.com\/embed\/QeicRf_Ri3Y&#8217; frameborder=&#8217;0&#8242; allowfullscreen&gt;&lt;\/iframe&gt;&lt;\/div&gt;<\/p>\n\n\n\n<p>If you have <a href=\"https:\/\/help.dreamhost.com\/hc\/en-us\/articles\/215089118\" target=\"_blank\" rel=\"noopener\">added an SSL certificate<\/a> to your domain, such as DreamHost\u2019s <a href=\"https:\/\/help.dreamhost.com\/hc\/en-us\/articles\/216539548\" target=\"_blank\" rel=\"noopener\">free Let\u2019s Encrypt certificate<\/a>, it\u2019s a good idea to force your site to <a href=\"https:\/\/help.dreamhost.com\/hc\/en-us\/articles\/215747758\" target=\"_blank\" rel=\"noopener\">load using HTTPS<\/a>. This will ensure that your site is safer for both you and your visitors.<\/p>\n\n\n\n<p>You can make it happen by adding the following code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">RewriteEngine On\nRewriteCond %{HTTPS} !=on\nRewriteRule ^(.*)$ https:\/\/%{HTTP_HOST}%{REQUEST_URI} [L,R=301]<\/pre>\n\n\n\n<p>Your site will now automatically redirect any HTTP requests and direct them to use HTTPS instead. For example, if a user tries to access <i>http:\/\/www.example.com<\/i>, they will be automatically redirected to <i>https:\/\/www.example.com<\/i>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-4-change-caching-settings\"><b>4. Change Caching Settings<\/b><\/h3>\n\n\n\n<p><a href=\"https:\/\/www.dreamhost.com\/blog\/caching-solutions-for-wordpress\/\" target=\"_blank\" rel=\"noopener\">Browser caching<\/a> is a process where certain website files are temporarily saved on a visitor\u2019s local device to enable pages to load faster. Using <i>.htaccess<\/i>, you can change the amount of time that your files are stored in the browser cache until they are updated with new versions.<\/p>\n\n\n\n<p>There are a <a href=\"https:\/\/help.dreamhost.com\/hc\/en-us\/articles\/216363157\" target=\"_blank\" rel=\"noopener\">few different ways<\/a> to do this, but for this example, we\u2019ll use a function called mod_headers. The following code will change the maximum caching time for all <i>jpg<\/i>, <i>jpeg<\/i>, <i>png<\/i>, and <i>gif<\/i> files:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;ifModule mod_headers.c&gt;\n&lt;filesMatch \".(jpg|jpeg|png|gif)$\"&gt;\nHeader set Cache-Control \"max-age=2592000, public\"\n&lt;\/filesMatch&gt;<\/pre>\n\n\n\n<p>We\u2019ve set the maximum time to 2,592,000 seconds, which equates to 30 days. You can change this amount if you want, as well as the file extensions that will be affected. If you want to add different settings for different extensions, simply add more mod_header functions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-5-prevent-certain-script-injection-attacks\"><b>5. Prevent Certain Script Injection Attacks<\/b><\/h3>\n\n\n\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Code_injection\" target=\"_blank\" rel=\"noopener\">Script injection<\/a> (or \u2018code injection\u2019) attacks attempt to change how a site or application executes by adding invalid code. For example, someone might add a script to a text field on your site and then submit it, which could cause your site to actually run the script.<\/p>\n\n\n\n<p>You can add <a href=\"https:\/\/gist.github.com\/unix7\/1733822\" target=\"_blank\" rel=\"noopener\">the following code<\/a> to protect against certain types of script injection:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Options +FollowSymLinks\nRewriteEngine On\nRewriteCond %{QUERY_STRING} (&lt;|%3C).*script.*(&gt;|%3E) [NC,OR]\nRewriteCond %{QUERY_STRING} GLOBALS(=|[|%[0-9A-Z]{0,2}) [OR]\nRewriteCond %{QUERY_STRING} _REQUEST(=|[|%[0-9A-Z]{0,2})\nRewriteRule ^(.*)$ index.php [F,L]<\/pre>\n\n\n\n<p>Your site should now be able to detect and stop script injection attempts and redirect the culprit to your <i>index.php<\/i> page.<\/p>\n\n\n\n<p>However, it\u2019s important to note that this example will not protect against all types of injection attacks. While this particular code can certainly be useful, you should not use it as <a href=\"https:\/\/help.dreamhost.com\/hc\/en-us\/articles\/215947927\" target=\"_blank\" rel=\"noopener\">your only protection<\/a> against this type of attack.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-6-stop-username-enumeration-attacks\"><b>6. Stop Username Enumeration Attacks<\/b><\/h3>\n\n\n\n<p>Username enumeration is a process where usernames from your site are harvested by looking at each user\u2019s author page. This is particularly problematic if someone manages to find your admin username, which makes it much easier for bots to gain access to your site.<\/p>\n\n\n\n<p>You can help prevent username enumeration by adding <a href=\"https:\/\/www.kevinleary.net\/preventing-possible-attempt-enumerate-users-solved\/\" target=\"_blank\" rel=\"noopener\">the following code<\/a>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">RewriteCond %{REQUEST_URI} !^\/wp-admin [NC]\nRewriteCond %{QUERY_STRING} author=d\nRewriteRule .* - [R=403,L]<\/pre>\n\n\n\n<p>This will stop certain attempts to enumerate usernames and throw up a 403 error page instead. Bear in mind that this will <i>not<\/i> prevent all enumeration, and you should test your security thoroughly. We also recommend strengthening your login page further by implementing <a href=\"https:\/\/help.dreamhost.com\/hc\/en-us\/articles\/216013897\" target=\"_blank\" rel=\"noopener\">Multifactor Authentication<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-7-prevent-image-hotlinking\"><b>7. Prevent Image Hotlinking<\/b><\/h3>\n\n\n\n<p><a href=\"https:\/\/themeisle.com\/blog\/prevent-image-hotlinking-in-wordpress\/\" target=\"_blank\" rel=\"noopener\">Image hotlinking<\/a> is a common problem that happens when images on your server are being displayed on another site. You can stop this by adding <a href=\"https:\/\/help.dreamhost.com\/hc\/en-us\/articles\/216363197\" target=\"_blank\" rel=\"noopener\">the following code<\/a> to <i>.htaccess<\/i>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">RewriteEngine On\nRewriteCond %{HTTP_REFERER} !^$\nRewriteCond %{HTTP_REFERER} !^https:\/\/(www.)?example.com\/.*$ [NC]\nRewriteRule .(png|gif|jpg|jpeg)$ https:\/\/www.example.com\/wp-content\/uploads\/hotlink.gif [R,L]<\/pre>\n\n\n\n<p>Replace <i>example.com<\/i> with your own domain, and this code will prevent images from loading on all other sites. Instead, the picture you specify on the last line will load. You can use this to send an alternative image to sites that try to display graphics from your server.<\/p>\n\n\n\n<p>Beware that this may cause issues when you might want images to appear externally, such as on search engines. You might also consider linking to a script instead of a static image, then respond with a watermarked image or an image containing an ad.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-8-control-your-file-extensions\"><b>8. Control Your File Extensions<\/b><\/h3>\n\n\n\n<p>By using <i>.htaccess<\/i>, you can control how files of different extensions are loaded by your site. There\u2019s a lot you can do with this feature, such as <a href=\"https:\/\/help.dreamhost.com\/hc\/en-us\/articles\/214200788\" target=\"_blank\" rel=\"noopener\">running files as PHP<\/a>, but we\u2019re just going to look at a basic example for now.<\/p>\n\n\n\n<p>The following code will <a href=\"https:\/\/help.dreamhost.com\/hc\/en-us\/articles\/215747728\" target=\"_blank\" rel=\"noopener\">remove the file extension from PHP files<\/a> when they\u2019re loaded. You can use this with any file type, as long as you replace all instances of \u201cphp\u201d with the extension you want:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">RewriteEngine On\nRewriteCond %{THE_REQUEST} ^[A-Z]{3,9} \/.*index HTTP\/\nRewriteRule ^(.*)index$ http:\/\/example.com\/$1 [L,R=301]\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule ^([^\/]+)\/$ http:\/\/example.com\/$1 [L,R=301]\nRewriteCond %{THE_REQUEST} ^[A-Z]{3,9} \/(.+).php HTTP\/\nRewriteRule ^(.+).php$ http:\/\/example.com\/$1 [L,R=301]\nRewriteRule ^([a-z]+)$ \/$1.php [L]<\/pre>\n\n\n\n<p>This will cause all PHP files to load without displaying their extension in the URL. For example, the <i>index.php<\/i> file will appear as just <i>index<\/i>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-9-force-files-to-download\"><b>9. Force Files to Download<\/b><\/h3>\n\n\n\n<p>Finally, when a file is requested on your site, the default behavior is to display it in the browser. For example, if you\u2019re hosting an audio file, it will start to play in the browser rather than being saved to the visitor\u2019s computer.<\/p>\n\n\n\n<p>You can change this by <a href=\"https:\/\/help.dreamhost.com\/hc\/en-us\/articles\/216363147\" target=\"_blank\" rel=\"noopener\">forcing the site to download the file<\/a> instead. This can be done with the following code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">AddType application\/octet-stream mp3<\/pre>\n\n\n\n<p>In this example, we\u2019ve used <i>mp3<\/i> files, but you can use the same function for <i>txt<\/i>, <i>mov<\/i>, or any other relevant extension.<\/p>\n\n\n\n<h2 id=\"h-improve-your-site-s-security-and-performance\" class=\"wp-block-heading\"><b>Improve Your Site\u2019s Security and Performance<\/b><\/h2>\n\n\n\n<p>The <a href=\"https:\/\/help.dreamhost.com\/hc\/en-us\/articles\/216456227\" target=\"_blank\" rel=\"noopener\"><i>.htaccess<\/i> file<\/a> provides flexibility for controlling how your web server behaves. You can also use it to increase your site\u2019s performance and get more control over exactly who can access what information.<\/p>\n\n\n\n<p>With <i>.htaccess<\/i>, you can deny access to particular parts of your website. Additionally, it allows you to redirect URLs, force your site to load over HTTPS, and prevent some script injection attacks.<\/p>\n\n\n\n<p>Editing your <i>.htaccess<\/i> file is just one way to improve your site\u2019s security. Choosing a <a href=\"https:\/\/www.dreamhost.com\/wordpress\/\" target=\"_blank\" rel=\"noopener\">secure WordPress hosting provider<\/a> is another. Check out our DreamPress managed hosting plans to see how we can boost your website\u2019s security and performance!<\/p>\n\n\n\n\n\n<div\n\tclass=\"article-cta-shared article-cta-small\"\n>\n    <div class=\"tr-img-wrap-outer jsLoading\"><img decoding=\"async\" class=\"js-img-lazy \" src=\"https:\/\/www-dev.dreamhost.com\/blog\/wp-content\/themes\/blog2018\/assets\/img\/lazy-loading-transparent.webp\" data-srcset=\"https:\/\/www-dev.dreamhost.com\/blog\/wp-content\/themes\/blog2018\/assets\/img\/article-small-ad-cta-placeholder.webp 1x, https:\/\/www-dev.dreamhost.com\/blog\/wp-content\/themes\/blog2018\/assets\/img\/article-small-ad-cta-placeholder-x2.webp 2x\"  alt=\"Ad background image\" \/><\/div>\n\n\n\n\n  <div class=\"content-btm\">\n    <h2\n      class=\"h2--md\"\n    >\n      Do More with DreamPress\n    <\/h2>\n    <p\n      class=\"p--md\"\n    >\n      DreamPress Plus and Pro users get access to Jetpack Professional (and 200+ premium themes) at no added cost!\n    <\/p>\n\n            <a\n            href=\"https:\/\/www.dreamhost.com\/wordpress\/managed\/\"\n                        class=\"btn btn--white-outline btn--sm btn--round\"\n                                    target=\"_blank\"\n            rel=\"noopener noreferrer\"\n            >\n                            Check Out Plans                    <\/a>\n\n  <\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Keeping your site safe should be a top priority for every administrator. WordPress is a secure platform out of the box, but that doesn\u2019t mean it\u2019s impervious to attacks. Fortunately, even if you aren\u2019t a security expert, you can use a file called .htaccess to harden your site\u2019s security policies. .htaccess is a configuration file [&hellip;]<\/p>\n","protected":false},"author":1062,"featured_media":37767,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_yoast_wpseo_metadesc":"In this guide, we\u2019ll introduce you to the .htaccess file, explain how it works, and discuss how to edit it. Read on to learn more!","toc_headlines":"[[\"h-an-introduction-to-the-htaccess-file\",\"<b>An Introduction to the .htaccess File<\/b>\"],[\"h-how-to-edit-your-wordpress-htaccess-file\",\"<b>How to Edit Your WordPress .htaccess File<\/b>\"],[\"h-9-things-you-can-do-with-your-wordpress-htaccess-file\",\"<b>9 Things You Can Do With Your WordPress .htaccess File<\/b>\"],[\"h-improve-your-site-s-security-and-performance\",\"<b>Improve Your Site\u2019s Security and Performance<\/b>\"]]","hide_toc":false,"footnotes":""},"categories":[13129,550,696],"tags":[],"class_list":["post-37762","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developer-central","category-tutorials","category-wordpress"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.3 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Beginner\u2019s Guide to the WordPress .htaccess File - DreamHost<\/title>\n<meta name=\"description\" content=\"In this guide, we\u2019ll introduce you to the .htaccess file, explain how it works, and discuss how to edit it. Read on to learn more!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www-dev.dreamhost.com\/blog\/guide-to-wp-and-htaccess\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Beginner\u2019s Guide to the WordPress .htaccess File\" \/>\n<meta property=\"og:description\" content=\"In this guide, we\u2019ll introduce you to the .htaccess file, explain how it works, and discuss how to edit it. Read on to learn more!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www-dev.dreamhost.com\/blog\/guide-to-wp-and-htaccess\/\" \/>\n<meta property=\"og:site_name\" content=\"DreamHost Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DreamHost\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-28T14:00:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-22T01:01:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/WordPress-.htaccess-File-Social.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Jason Cosper\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@dreamhost\" \/>\n<meta name=\"twitter:site\" content=\"@dreamhost\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jason Cosper\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Beginner\u2019s Guide to the WordPress .htaccess File - DreamHost","description":"In this guide, we\u2019ll introduce you to the .htaccess file, explain how it works, and discuss how to edit it. Read on to learn more!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www-dev.dreamhost.com\/blog\/guide-to-wp-and-htaccess\/","og_locale":"en_US","og_type":"article","og_title":"Beginner\u2019s Guide to the WordPress .htaccess File","og_description":"In this guide, we\u2019ll introduce you to the .htaccess file, explain how it works, and discuss how to edit it. Read on to learn more!","og_url":"https:\/\/www-dev.dreamhost.com\/blog\/guide-to-wp-and-htaccess\/","og_site_name":"DreamHost Blog","article_publisher":"https:\/\/www.facebook.com\/DreamHost\/","article_published_time":"2022-10-28T14:00:37+00:00","article_modified_time":"2025-05-22T01:01:16+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/WordPress-.htaccess-File-Social.jpg","type":"image\/jpeg"}],"author":"Jason Cosper","twitter_card":"summary_large_image","twitter_creator":"@dreamhost","twitter_site":"@dreamhost","twitter_misc":{"Written by":"Jason Cosper","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www-dev.dreamhost.com\/blog\/guide-to-wp-and-htaccess\/#article","isPartOf":{"@id":"https:\/\/www-dev.dreamhost.com\/blog\/guide-to-wp-and-htaccess\/"},"author":{"name":"Jason Cosper","@id":"https:\/\/www-dev.dreamhost.com\/blog\/#\/schema\/person\/2273b8bf7336df78f6d99a89c17253c4"},"headline":"Beginner\u2019s Guide to the WordPress .htaccess File","datePublished":"2022-10-28T14:00:37+00:00","dateModified":"2025-05-22T01:01:16+00:00","mainEntityOfPage":{"@id":"https:\/\/www-dev.dreamhost.com\/blog\/guide-to-wp-and-htaccess\/"},"wordCount":1837,"publisher":{"@id":"https:\/\/www-dev.dreamhost.com\/blog\/#organization"},"image":{"@id":"https:\/\/www-dev.dreamhost.com\/blog\/guide-to-wp-and-htaccess\/#primaryimage"},"thumbnailUrl":"https:\/\/www-dev.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/WordPress-.htaccess-File-Feature.jpg","articleSection":["Developer Central","Tutorials","WordPress"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www-dev.dreamhost.com\/blog\/guide-to-wp-and-htaccess\/","url":"https:\/\/www-dev.dreamhost.com\/blog\/guide-to-wp-and-htaccess\/","name":"Beginner\u2019s Guide to the WordPress .htaccess File - DreamHost","isPartOf":{"@id":"https:\/\/www-dev.dreamhost.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www-dev.dreamhost.com\/blog\/guide-to-wp-and-htaccess\/#primaryimage"},"image":{"@id":"https:\/\/www-dev.dreamhost.com\/blog\/guide-to-wp-and-htaccess\/#primaryimage"},"thumbnailUrl":"https:\/\/www-dev.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/WordPress-.htaccess-File-Feature.jpg","datePublished":"2022-10-28T14:00:37+00:00","dateModified":"2025-05-22T01:01:16+00:00","description":"In this guide, we\u2019ll introduce you to the .htaccess file, explain how it works, and discuss how to edit it. Read on to learn more!","breadcrumb":{"@id":"https:\/\/www-dev.dreamhost.com\/blog\/guide-to-wp-and-htaccess\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www-dev.dreamhost.com\/blog\/guide-to-wp-and-htaccess\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www-dev.dreamhost.com\/blog\/guide-to-wp-and-htaccess\/#primaryimage","url":"https:\/\/www-dev.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/WordPress-.htaccess-File-Feature.jpg","contentUrl":"https:\/\/www-dev.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/WordPress-.htaccess-File-Feature.jpg","width":900,"height":598,"caption":"editing .htaccess in WordPress"},{"@type":"BreadcrumbList","@id":"https:\/\/www-dev.dreamhost.com\/blog\/guide-to-wp-and-htaccess\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/dhblog.dream.press\/blog\/"},{"@type":"ListItem","position":2,"name":"Beginner\u2019s Guide to the WordPress .htaccess File"}]},{"@type":"WebSite","@id":"https:\/\/www-dev.dreamhost.com\/blog\/#website","url":"https:\/\/www-dev.dreamhost.com\/blog\/","name":"DreamHost Blog","description":"","publisher":{"@id":"https:\/\/www-dev.dreamhost.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www-dev.dreamhost.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www-dev.dreamhost.com\/blog\/#organization","name":"DreamHost","url":"https:\/\/www-dev.dreamhost.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www-dev.dreamhost.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/dhblog.dream.press\/blog\/wp-content\/uploads\/2019\/01\/dh_logo-blue-2.png","contentUrl":"https:\/\/dhblog.dream.press\/blog\/wp-content\/uploads\/2019\/01\/dh_logo-blue-2.png","width":1200,"height":168,"caption":"DreamHost"},"image":{"@id":"https:\/\/www-dev.dreamhost.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DreamHost\/","https:\/\/x.com\/dreamhost","https:\/\/www.instagram.com\/dreamhost\/","https:\/\/www.linkedin.com\/company\/dreamhost\/","https:\/\/www.youtube.com\/user\/dreamhostusa"]},{"@type":"Person","@id":"https:\/\/www-dev.dreamhost.com\/blog\/#\/schema\/person\/2273b8bf7336df78f6d99a89c17253c4","name":"Jason Cosper","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www-dev.dreamhost.com\/blog\/wp-content\/uploads\/2022\/02\/IMG_4807-scaled.jpeg","url":"https:\/\/www-dev.dreamhost.com\/blog\/wp-content\/uploads\/2022\/02\/IMG_4807-scaled.jpeg","contentUrl":"https:\/\/www-dev.dreamhost.com\/blog\/wp-content\/uploads\/2022\/02\/IMG_4807-scaled.jpeg","caption":"Jason Cosper"},"description":"Jason is DreamHost\u2019s WordPress Product Advocate, based out of Bakersfield, CA. He is currently working on making our DreamPress product even better. In his free time, he likes to curl up on the couch and watch scary movies with his wife Sarah and three very small dogs. Follow him on Twitter.","url":"https:\/\/www-dev.dreamhost.com\/blog\/author\/jasoncosper\/"}]}},"lang":"en","translations":{"en":37762,"es":37785,"pt":52508,"de":53999,"pl":54005,"uk":54037,"ru":54399,"it":68094,"fr":69779,"nl":69803},"pll_sync_post":[],"_links":{"self":[{"href":"https:\/\/www-dev.dreamhost.com\/blog\/wp-json\/wp\/v2\/posts\/37762","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www-dev.dreamhost.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www-dev.dreamhost.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www-dev.dreamhost.com\/blog\/wp-json\/wp\/v2\/users\/1062"}],"replies":[{"embeddable":true,"href":"https:\/\/www-dev.dreamhost.com\/blog\/wp-json\/wp\/v2\/comments?post=37762"}],"version-history":[{"count":8,"href":"https:\/\/www-dev.dreamhost.com\/blog\/wp-json\/wp\/v2\/posts\/37762\/revisions"}],"predecessor-version":[{"id":61505,"href":"https:\/\/www-dev.dreamhost.com\/blog\/wp-json\/wp\/v2\/posts\/37762\/revisions\/61505"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www-dev.dreamhost.com\/blog\/wp-json\/wp\/v2\/media\/37767"}],"wp:attachment":[{"href":"https:\/\/www-dev.dreamhost.com\/blog\/wp-json\/wp\/v2\/media?parent=37762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www-dev.dreamhost.com\/blog\/wp-json\/wp\/v2\/categories?post=37762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www-dev.dreamhost.com\/blog\/wp-json\/wp\/v2\/tags?post=37762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}