{"id":535,"date":"2010-05-29T22:40:59","date_gmt":"2010-05-30T05:40:59","guid":{"rendered":"http:\/\/www.bryanhadaway.com\/?p=535"},"modified":"2025-05-22T15:31:45","modified_gmt":"2025-05-22T22:31:45","slug":"dynamic-seo-friendly-htaccess-file","status":"publish","type":"post","link":"https:\/\/webguy.io\/blog\/dynamic-seo-friendly-htaccess-file\/","title":{"rendered":"Dynamic SEO-Friendly .htaccess File"},"content":{"rendered":"<h2>The Code<\/h2>\n<p>Copy the following to a plain text editor and save as <em>.htaccess<\/em>. Then, via FTP, upload to the root folder of your website (usually called <em>public_html<\/em>). It will be suitable for most hosting environments, but not all. You can find the website template I&#8217;ve also written which contains this .htaccess example <a href=\"https:\/\/generic.tools\/\">here<\/a>.<\/p>\n<pre><code>&lt;files .htaccess&gt;\r\norder allow,deny\r\ndeny from all\r\n&lt;\/files&gt;\r\n\r\noptions all -indexes\r\n\r\nrewriteengine on\r\n\r\nrewritecond %{http_host} ^www\\.(.*)$ [nc]\r\nrewriterule ^(.*)$ http:\/\/%1\/$1 [r=301,l]\r\n\r\nrewriterule ^\/?(.*\/?)index\\.(htm|html|php) \/$1 [r=301,l]\r\n\r\nrewritecond %{request_filename} !-d\r\nrewritecond %{request_filename}\\.php -f\r\nrewriterule ^(.*)$ $1.php\r\n\r\nerrordocument 404 \/404<\/code><\/pre>\n<h2>Each Section Explained<\/h2>\n<p>A description of the purpose of each section so that you understand better what&#8217;s going on and also so that you can remove any bits you don&#8217;t need.<\/p>\n<pre><code>&lt;files .htaccess&gt;\r\norder allow,deny\r\ndeny from all\r\n&lt;\/files&gt;<\/code><\/pre>\n<p>Protects the .htaccess file itself from unauthorized access.<\/p>\n<pre><code>options all -indexes<\/code><\/pre>\n<p>Disables directory indexes so that no one can snoop the contents of folders on your server.<\/p>\n<pre><code>rewritecond %{http_host} ^www\\.(.*)$ [nc]\r\nrewriterule ^(.*)$ http:\/\/%1\/$1 [r=301,l]<\/code><\/pre>\n<p>Redirects your site to all non-<strong>www.<\/strong> URLs. So, from <em>http:\/\/www.example.com\/<\/em> to <em>http:\/\/example.com\/<\/em>.<\/p>\n<pre><code>rewriterule ^\/?(.*\/?)index\\.(htm|html|php) \/$1 [r=301,l]<\/code><\/pre>\n<p>Ensures that your homepage is always reached at the proper <em>http:\/\/example.com\/<\/em> as opposed to <em>http:\/\/example.com\/index.php<\/em>.<\/p>\n<pre><code>rewritecond %{request_filename} !-d\r\nrewritecond %{request_filename}\\.php -f\r\nrewriterule ^(.*)$ $1.php<\/code><\/pre>\n<p>Allows for cleaner, friendlier URLs. For example: <em>http:\/\/example.com\/contact<\/em> as opposed to <em>http:\/\/example.com\/contact.php<\/em>.<\/p>\n<pre><code>errordocument 404 \/404<\/code><\/pre>\n<p>If a visitor types a URL incorrectly, clicked on a link with an error in it, or a page they&#8217;re trying to reach no longer exists, instead of seeing a generic error message, can be redirected to a custom error page.<\/p>\n<h2>Security<\/h2>\n<p>.htaccess is very powerful. You can do a great deal to protect your site from vulnerabilities and spam with it. I highly recommend checking out some of the <a href=\"https:\/\/perishablepress.com\/tag\/blacklist\/\">blacklists<\/a> Perishable Press has to offer.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Code Copy the following to a plain text editor and save as .htaccess. Then, via FTP, upload to the root folder of your website (usually called public_html). It will be suitable for most hosting environments, but not all. You can find the website template I&#8217;ve also written which contains this .htaccess example here. &lt;files <a href=\"https:\/\/webguy.io\/blog\/dynamic-seo-friendly-htaccess-file\/\" class=\"more-link\">&#8230;<span class=\"screen-reader-text\">  Dynamic SEO-Friendly .htaccess File<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39,47,4,5],"tags":[239,208,247,205,206],"class_list":["post-535","post","type-post","status-publish","format-standard","hentry","category-code","category-programming","category-seo","category-web-design","tag-code","tag-google","tag-programming","tag-seo","tag-web-design"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/webguy.io\/blog\/wp-json\/wp\/v2\/posts\/535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webguy.io\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webguy.io\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webguy.io\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webguy.io\/blog\/wp-json\/wp\/v2\/comments?post=535"}],"version-history":[{"count":0,"href":"https:\/\/webguy.io\/blog\/wp-json\/wp\/v2\/posts\/535\/revisions"}],"wp:attachment":[{"href":"https:\/\/webguy.io\/blog\/wp-json\/wp\/v2\/media?parent=535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webguy.io\/blog\/wp-json\/wp\/v2\/categories?post=535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webguy.io\/blog\/wp-json\/wp\/v2\/tags?post=535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}