{"id":309,"date":"2026-04-30T22:03:54","date_gmt":"2026-04-30T22:03:54","guid":{"rendered":"https:\/\/webhosting.school\/blog\/?p=309"},"modified":"2026-04-30T22:03:55","modified_gmt":"2026-04-30T22:03:55","slug":"htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration","status":"publish","type":"post","link":"https:\/\/webhosting.school\/blog\/web-servers\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\/","title":{"rendered":".htaccess Cheatsheet: The Ultimate Guide to Mastering Apache Configuration"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">The Ultimate <code>.htaccess<\/code> Cheat Sheet: Commands, Tricks, and Real-World Uses<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>.htaccess<\/code> file is one of the most powerful\u2014and often misunderstood\u2014tools available to developers and server administrators using Apache web servers. Despite being just a simple text file, it can control how your website behaves in profound ways: from redirecting URLs and securing directories to improving performance and customizing error pages.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This guide is designed as a practical, beginner-friendly yet comprehensive cheat sheet. Instead of just explaining concepts, it shows you <em>exactly what commands you can use<\/em> and how they work.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">What Is a <code>.htaccess<\/code> File?<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\"><code>.htaccess<\/code> stands for \u201chypertext access.\u201d It is a configuration file used by Apache-based web servers that allows you to override server settings on a per-directory basis.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Unlike editing the main server config (<code>httpd.conf<\/code>), <code>.htaccess<\/code> gives you control without requiring root\/server access\u2014making it especially useful for shared hosting environments.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">Where to Place the <code>.htaccess<\/code> File<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Typically placed in your website\u2019s <strong>root directory<\/strong> (<code>public_html<\/code> or <code>www<\/code>)<\/li>\n\n\n\n<li>Affects the directory it is in <strong>and all subdirectories<\/strong><\/li>\n\n\n\n<li>Must be named exactly:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>.htaccess\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">(no filename, just the extension)<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">Enable <code>.htaccess<\/code> (If Not Working)<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">In Apache\u2019s main config:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>AllowOverride All\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">Core Modules Used in <code>.htaccess<\/code><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Before diving into commands, know these commonly used modules:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>mod_rewrite<\/code> \u2192 URL rewriting and redirects<\/li>\n\n\n\n<li><code>mod_headers<\/code> \u2192 control HTTP headers<\/li>\n\n\n\n<li><code>mod_expires<\/code> \u2192 caching<\/li>\n\n\n\n<li><code>mod_auth<\/code> \u2192 authentication<\/li>\n\n\n\n<li><code>mod_deflate<\/code> \u2192 compression<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">1. URL Redirection Cheat Sheet<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Redirect a Page (301 Permanent)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Redirect 301 \/old-page.html https:\/\/example.com\/new-page.html\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Redirect Entire Site to HTTPS<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>RewriteEngine On\nRewriteCond %{HTTPS} off\nRewriteRule ^(.*)$ https:\/\/%{HTTP_HOST}\/$1 &#91;R=301,L]\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Redirect Non-WWW to WWW<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>RewriteEngine On\nRewriteCond %{HTTP_HOST} ^example.com &#91;NC]\nRewriteRule ^(.*)$ https:\/\/www.example.com\/$1 &#91;L,R=301]\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Redirect WWW to Non-WWW<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>RewriteEngine On\nRewriteCond %{HTTP_HOST} ^www.example.com &#91;NC]\nRewriteRule ^(.*)$ https:\/\/example.com\/$1 &#91;L,R=301]\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Redirect Entire Domain<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Redirect 301 \/ https:\/\/newdomain.com\/\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">2. URL Rewriting Cheat Sheet<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Enable Rewrite Engine<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>RewriteEngine On\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Clean URLs (Remove .php Extension)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>RewriteCond %{REQUEST_FILENAME} !-d\nRewriteCond %{REQUEST_FILENAME}.php -f\nRewriteRule ^(.*)$ $1.php &#91;L]\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Pretty URLs<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>RewriteRule ^blog\/(&#91;0-9]+)\/?$ blog.php?id=$1 &#91;L]\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Force Trailing Slash<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>RewriteCond %{REQUEST_FILENAME} !-f\nRewriteRule ^(.*&#91;^\/])$ \/$1\/ &#91;L,R=301]\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">3. Password Protection (Basic Auth)<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Protect a Directory<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>AuthType Basic\nAuthName \"Restricted Area\"\nAuthUserFile \/full\/path\/.htpasswd\nRequire valid-user\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example <code>.htpasswd<\/code> Entry<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>username:$apr1$random$hashedpassword\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">4. Block Access Cheat Sheet<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Block Specific IP<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Deny from 192.168.1.1\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Allow Only One IP<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Order Deny,Allow\nDeny from all\nAllow from 123.123.123.123\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Block Multiple IPs<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Deny from 111.111.111.111\nDeny from 222.222.222.222\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Block a Country (basic example)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SetEnvIf GEOIP_COUNTRY_CODE CN BlockCountry\nDeny from env=BlockCountry\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">5. Hotlink Protection<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Prevent others from embedding your images:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RewriteEngine On\nRewriteCond %{HTTP_REFERER} !^$\nRewriteCond %{HTTP_REFERER} !^https:\/\/(www\\.)?example.com\/ &#91;NC]\nRewriteRule \\.(jpg|jpeg|png|gif)$ - &#91;F]\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">6. Custom Error Pages<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">404 Page<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ErrorDocument 404 \/errors\/404.html\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">403 Page<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ErrorDocument 403 \/errors\/403.html\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">500 Page<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ErrorDocument 500 \/errors\/500.html\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">7. Caching and Performance<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Enable Browser Caching<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;IfModule mod_expires.c&gt;\nExpiresActive On\nExpiresByType image\/jpg \"access 1 year\"\nExpiresByType text\/css \"access 1 month\"\nExpiresByType application\/javascript \"access 1 month\"\n&lt;\/IfModule&gt;\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Disable Caching<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;FilesMatch \"\\.(html|php)$\"&gt;\nHeader set Cache-Control \"no-store, no-cache, must-revalidate\"\n&lt;\/FilesMatch&gt;\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">8. GZIP Compression<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;IfModule mod_deflate.c&gt;\nAddOutputFilterByType DEFLATE text\/html text\/plain text\/css application\/javascript\n&lt;\/IfModule&gt;\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">9. Security Hardening<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Disable Directory Browsing<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Options -Indexes\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Prevent Access to <code>.htaccess<\/code><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;Files .htaccess&gt;\nOrder allow,deny\nDeny from all\n&lt;\/Files&gt;\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Block Access to Sensitive Files<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;FilesMatch \"(config|db|ini|log)\"&gt;\nDeny from all\n&lt;\/FilesMatch&gt;\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Disable Script Execution in Uploads<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;Directory \"\/uploads\"&gt;\nphp_flag engine off\n&lt;\/Directory&gt;\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">10. MIME Types<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Add MIME Type<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>AddType application\/json .json\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Force Download<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>AddType application\/octet-stream .pdf\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">11. File Handling<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Default File<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>DirectoryIndex index.php index.html\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Force Download of Files<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;Files \"*.zip\"&gt;\nForceType application\/octet-stream\nHeader set Content-Disposition attachment\n&lt;\/Files&gt;\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">12. CORS (Cross-Origin Requests)<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>Header set Access-Control-Allow-Origin \"*\"\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">13. Prevent Spam and Bad Bots<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Block User Agents<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>RewriteEngine On\nRewriteCond %{HTTP_USER_AGENT} BadBot &#91;NC]\nRewriteRule .* - &#91;F,L]\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Block Empty User Agents<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>RewriteCond %{HTTP_USER_AGENT} ^-?$\nRewriteRule ^ - &#91;F]\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">14. Force File Download Dialog<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>Header set Content-Disposition attachment\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">15. Redirect Based on Browser<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>RewriteCond %{HTTP_USER_AGENT} Chrome\nRewriteRule ^$ chrome.html &#91;L]\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">16. Time-Based Redirects<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>RewriteCond %{TIME_HOUR} ^23$\nRewriteRule ^$ night.html &#91;L]\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">17. Prevent Image Indexing by Google<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>Header set X-Robots-Tag \"noindex, noimageindex\"\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">18. Set PHP Settings<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>php_value upload_max_filesize 64M\nphp_value post_max_size 64M\nphp_value max_execution_time 300\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">19. Disable ETags<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>FileETag None\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">20. Remove File Extensions<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>RewriteEngine On\nRewriteCond %{REQUEST_FILENAME}.html -f\nRewriteRule ^(.*)$ $1.html &#91;L]\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">21. Force UTF-8 Encoding<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>AddDefaultCharset UTF-8\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">22. Redirect Based on Referrer<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>RewriteCond %{HTTP_REFERER} example.com\nRewriteRule .* - &#91;F]\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">23. Limit Request Size<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>LimitRequestBody 102400\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">24. Protect Against Clickjacking<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>Header always append X-Frame-Options SAMEORIGIN\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">25. XSS Protection<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>Header set X-XSS-Protection \"1; mode=block\"\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">26. Content Security Policy<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>Header set Content-Security-Policy \"default-src 'self';\"\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">27. Prevent MIME Sniffing<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>Header set X-Content-Type-Options \"nosniff\"\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">28. Redirect Mobile Users<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>RewriteCond %{HTTP_USER_AGENT} \"iPhone|Android\" &#91;NC]\nRewriteRule ^$ mobile.html &#91;L]\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">29. Deny Access by File Type<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;FilesMatch \"\\.(exe|sh|bat)$\"&gt;\nDeny from all\n&lt;\/FilesMatch&gt;\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">30. Logging and Debugging<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Enable Rewrite Logging (older Apache)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>RewriteLog \"\/path\/to\/rewrite.log\"\nRewriteLogLevel 3\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">Best Practices<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Keep It Clean<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Only include what you need<\/li>\n\n\n\n<li>Too many rules can slow your server<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Test Carefully<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>One wrong rule can break your site<\/li>\n\n\n\n<li>Always back up your <code>.htaccess<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Use 301 Redirects for SEO<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Preserve ranking and authority<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Avoid Overusing <code>.htaccess<\/code><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use main server config when possible (faster)<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">Common Mistakes<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Missing <code>RewriteEngine On<\/code><\/li>\n\n\n\n<li>Incorrect file paths<\/li>\n\n\n\n<li>Infinite redirect loops<\/li>\n\n\n\n<li>Case sensitivity issues<\/li>\n\n\n\n<li>Forgetting to enable required modules<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">Final Thoughts<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>.htaccess<\/code> file is like a Swiss Army knife for your web server. It gives you control over routing, security, performance, and behavior\u2014all without touching the main server configuration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For beginners, it might feel overwhelming at first. But once you start using a few key commands\u2014like redirects, rewrites, and security rules\u2014you\u2019ll quickly see how powerful it is.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For advanced users, mastering <code>.htaccess<\/code> opens the door to fine-grained control that can significantly improve your website\u2019s speed, security, and SEO performance.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>The Ultimate .htaccess Cheat Sheet: Commands, Tricks, and Real-World Uses The .htaccess file is one of the most powerful\u2014and often misunderstood\u2014tools available to developers and server administrators using Apache web&#8230; <\/p>\n","protected":false},"author":1,"featured_media":310,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[20],"tags":[],"class_list":["post-309","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-servers"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>.htaccess Cheatsheet: The Ultimate Guide to Mastering Apache Configuration - Website and Web Hosting School<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webhosting.school\/blog\/web-servers\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\".htaccess Cheatsheet: The Ultimate Guide to Mastering Apache Configuration - Website and Web Hosting School\" \/>\n<meta property=\"og:description\" content=\"The Ultimate .htaccess Cheat Sheet: Commands, Tricks, and Real-World Uses The .htaccess file is one of the most powerful\u2014and often misunderstood\u2014tools available to developers and server administrators using Apache web...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webhosting.school\/blog\/web-servers\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\/\" \/>\n<meta property=\"og:site_name\" content=\"Website and Web Hosting School\" \/>\n<meta property=\"article:published_time\" content=\"2026-04-30T22:03:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-30T22:03:55+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/webhosting.school\/blog\/wp-content\/uploads\/2026\/04\/image.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"784\" \/>\n\t<meta property=\"og:image:height\" content=\"1168\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Brian Modansky\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Brian Modansky\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/web-servers\\\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/web-servers\\\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\\\/\"},\"author\":{\"name\":\"Brian Modansky\",\"@id\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/#\\\/schema\\\/person\\\/effebf9156e7d1e5d99df1c9681ee5a2\"},\"headline\":\".htaccess Cheatsheet: The Ultimate Guide to Mastering Apache Configuration\",\"datePublished\":\"2026-04-30T22:03:54+00:00\",\"dateModified\":\"2026-04-30T22:03:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/web-servers\\\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\\\/\"},\"wordCount\":564,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/web-servers\\\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/image.jpg\",\"articleSection\":[\"Web Servers\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/webhosting.school\\\/blog\\\/web-servers\\\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/web-servers\\\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\\\/\",\"url\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/web-servers\\\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\\\/\",\"name\":\".htaccess Cheatsheet: The Ultimate Guide to Mastering Apache Configuration - Website and Web Hosting School\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/web-servers\\\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/web-servers\\\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/image.jpg\",\"datePublished\":\"2026-04-30T22:03:54+00:00\",\"dateModified\":\"2026-04-30T22:03:55+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/web-servers\\\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/webhosting.school\\\/blog\\\/web-servers\\\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/web-servers\\\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\\\/#primaryimage\",\"url\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/image.jpg\",\"contentUrl\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/image.jpg\",\"width\":784,\"height\":1168},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/web-servers\\\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\".htaccess Cheatsheet: The Ultimate Guide to Mastering Apache Configuration\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/\",\"name\":\"Website and Web Hosting School Blog - WebHosting.school Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/#organization\",\"name\":\"Website and Web Hosting School Blog - WebHosting.school Blog\",\"url\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/06\\\/logo-dark.png\",\"contentUrl\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/06\\\/logo-dark.png\",\"width\":1017,\"height\":187,\"caption\":\"Website and Web Hosting School Blog - WebHosting.school Blog\"},\"image\":{\"@id\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/#\\\/schema\\\/person\\\/effebf9156e7d1e5d99df1c9681ee5a2\",\"name\":\"Brian Modansky\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/abc585c779577b715c0449210bc7616912b12f1887d4531b3040c155abfe1672?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/abc585c779577b715c0449210bc7616912b12f1887d4531b3040c155abfe1672?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/abc585c779577b715c0449210bc7616912b12f1887d4531b3040c155abfe1672?s=96&d=mm&r=g\",\"caption\":\"Brian Modansky\"},\"description\":\"With 23+ years in the Web Hosting Industry, Brian has had the opportunity to design websites for some of the largest companies in the industry. Brian currently holds the position as Founder, CEO, and Executive Creative Director for WebHosting.School.\",\"sameAs\":[\"https:\\\/\\\/webhosting.school\\\/blog\"],\"url\":\"https:\\\/\\\/webhosting.school\\\/blog\\\/author\\\/brian\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":".htaccess Cheatsheet: The Ultimate Guide to Mastering Apache Configuration - Website and Web Hosting School","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:\/\/webhosting.school\/blog\/web-servers\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\/","og_locale":"en_US","og_type":"article","og_title":".htaccess Cheatsheet: The Ultimate Guide to Mastering Apache Configuration - Website and Web Hosting School","og_description":"The Ultimate .htaccess Cheat Sheet: Commands, Tricks, and Real-World Uses The .htaccess file is one of the most powerful\u2014and often misunderstood\u2014tools available to developers and server administrators using Apache web...","og_url":"https:\/\/webhosting.school\/blog\/web-servers\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\/","og_site_name":"Website and Web Hosting School","article_published_time":"2026-04-30T22:03:54+00:00","article_modified_time":"2026-04-30T22:03:55+00:00","og_image":[{"width":784,"height":1168,"url":"https:\/\/webhosting.school\/blog\/wp-content\/uploads\/2026\/04\/image.jpg","type":"image\/jpeg"}],"author":"Brian Modansky","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Brian Modansky","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webhosting.school\/blog\/web-servers\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\/#article","isPartOf":{"@id":"https:\/\/webhosting.school\/blog\/web-servers\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\/"},"author":{"name":"Brian Modansky","@id":"https:\/\/webhosting.school\/blog\/#\/schema\/person\/effebf9156e7d1e5d99df1c9681ee5a2"},"headline":".htaccess Cheatsheet: The Ultimate Guide to Mastering Apache Configuration","datePublished":"2026-04-30T22:03:54+00:00","dateModified":"2026-04-30T22:03:55+00:00","mainEntityOfPage":{"@id":"https:\/\/webhosting.school\/blog\/web-servers\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\/"},"wordCount":564,"commentCount":0,"publisher":{"@id":"https:\/\/webhosting.school\/blog\/#organization"},"image":{"@id":"https:\/\/webhosting.school\/blog\/web-servers\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\/#primaryimage"},"thumbnailUrl":"https:\/\/webhosting.school\/blog\/wp-content\/uploads\/2026\/04\/image.jpg","articleSection":["Web Servers"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webhosting.school\/blog\/web-servers\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webhosting.school\/blog\/web-servers\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\/","url":"https:\/\/webhosting.school\/blog\/web-servers\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\/","name":".htaccess Cheatsheet: The Ultimate Guide to Mastering Apache Configuration - Website and Web Hosting School","isPartOf":{"@id":"https:\/\/webhosting.school\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/webhosting.school\/blog\/web-servers\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\/#primaryimage"},"image":{"@id":"https:\/\/webhosting.school\/blog\/web-servers\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\/#primaryimage"},"thumbnailUrl":"https:\/\/webhosting.school\/blog\/wp-content\/uploads\/2026\/04\/image.jpg","datePublished":"2026-04-30T22:03:54+00:00","dateModified":"2026-04-30T22:03:55+00:00","breadcrumb":{"@id":"https:\/\/webhosting.school\/blog\/web-servers\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webhosting.school\/blog\/web-servers\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webhosting.school\/blog\/web-servers\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\/#primaryimage","url":"https:\/\/webhosting.school\/blog\/wp-content\/uploads\/2026\/04\/image.jpg","contentUrl":"https:\/\/webhosting.school\/blog\/wp-content\/uploads\/2026\/04\/image.jpg","width":784,"height":1168},{"@type":"BreadcrumbList","@id":"https:\/\/webhosting.school\/blog\/web-servers\/htaccess-cheatsheet-the-ultimate-guide-to-mastering-apache-configuration\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webhosting.school\/blog\/"},{"@type":"ListItem","position":2,"name":".htaccess Cheatsheet: The Ultimate Guide to Mastering Apache Configuration"}]},{"@type":"WebSite","@id":"https:\/\/webhosting.school\/blog\/#website","url":"https:\/\/webhosting.school\/blog\/","name":"Website and Web Hosting School Blog - WebHosting.school Blog","description":"","publisher":{"@id":"https:\/\/webhosting.school\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webhosting.school\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webhosting.school\/blog\/#organization","name":"Website and Web Hosting School Blog - WebHosting.school Blog","url":"https:\/\/webhosting.school\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webhosting.school\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/webhosting.school\/blog\/wp-content\/uploads\/2024\/06\/logo-dark.png","contentUrl":"https:\/\/webhosting.school\/blog\/wp-content\/uploads\/2024\/06\/logo-dark.png","width":1017,"height":187,"caption":"Website and Web Hosting School Blog - WebHosting.school Blog"},"image":{"@id":"https:\/\/webhosting.school\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/webhosting.school\/blog\/#\/schema\/person\/effebf9156e7d1e5d99df1c9681ee5a2","name":"Brian Modansky","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/abc585c779577b715c0449210bc7616912b12f1887d4531b3040c155abfe1672?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/abc585c779577b715c0449210bc7616912b12f1887d4531b3040c155abfe1672?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/abc585c779577b715c0449210bc7616912b12f1887d4531b3040c155abfe1672?s=96&d=mm&r=g","caption":"Brian Modansky"},"description":"With 23+ years in the Web Hosting Industry, Brian has had the opportunity to design websites for some of the largest companies in the industry. Brian currently holds the position as Founder, CEO, and Executive Creative Director for WebHosting.School.","sameAs":["https:\/\/webhosting.school\/blog"],"url":"https:\/\/webhosting.school\/blog\/author\/brian\/"}]}},"_links":{"self":[{"href":"https:\/\/webhosting.school\/blog\/wp-json\/wp\/v2\/posts\/309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webhosting.school\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webhosting.school\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webhosting.school\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webhosting.school\/blog\/wp-json\/wp\/v2\/comments?post=309"}],"version-history":[{"count":2,"href":"https:\/\/webhosting.school\/blog\/wp-json\/wp\/v2\/posts\/309\/revisions"}],"predecessor-version":[{"id":312,"href":"https:\/\/webhosting.school\/blog\/wp-json\/wp\/v2\/posts\/309\/revisions\/312"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webhosting.school\/blog\/wp-json\/wp\/v2\/media\/310"}],"wp:attachment":[{"href":"https:\/\/webhosting.school\/blog\/wp-json\/wp\/v2\/media?parent=309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhosting.school\/blog\/wp-json\/wp\/v2\/categories?post=309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhosting.school\/blog\/wp-json\/wp\/v2\/tags?post=309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}