{"id":3688,"date":"2016-09-05T19:33:35","date_gmt":"2016-09-05T23:33:35","guid":{"rendered":"https:\/\/underhost.com\/blog\/?p=3688"},"modified":"2025-03-09T14:54:43","modified_gmt":"2025-03-09T18:54:43","slug":"how-to-redirect-your-domain-to-a-subdirectory-without-creating-a-loop","status":"publish","type":"post","link":"https:\/\/underhost.com\/blog\/how-to-redirect-your-domain-to-a-subdirectory-without-creating-a-loop\/","title":{"rendered":"How to Redirect Your Domain to a Subdirectory Without Creating a Loop"},"content":{"rendered":"\r\n<p>If you want to redirect <strong>http:\/\/www.yourdomain.com<\/strong> to <strong>http:\/\/www.yourdomain.com\/subdirectory\/<\/strong>, you need a specific <strong>.htaccess<\/strong> setup to prevent an infinite redirect loop.<\/p>\r\n\r\n\r\n<h2>Why Use .htaccess for Redirects?<\/h2>\r\n<p>The <code>.htaccess<\/code> file is a powerful configuration tool that controls redirections, URL rewrites, and access permissions on an Apache server. Setting up the correct redirect prevents your website from constantly looping back to the same URL.<\/p>\r\n\r\n<h2>.htaccess Code for Redirecting to a Subdirectory<\/h2>\r\n<p>Add the following code to the <code>.htaccess<\/code> file in the root directory of your website:<\/p>\r\n\r\n<blockquote>\r\n<pre><code>RewriteEngine on\r\nRewriteCond %{HTTP_HOST} ^(www.)?yourdomain.com$\r\nRewriteCond %{REQUEST_URI} !^\/newfolder\/\r\nRewriteCond %{REQUEST_FILENAME} !-f\r\nRewriteCond %{REQUEST_FILENAME} !-d\r\nRewriteRule ^(.*)$ \/newfolder\/$1 [L]\r\nRewriteCond %{HTTP_HOST} ^(www.)?yourdomain.com$\r\nRewriteRule ^(\/)?$ newfolder\/index.php [L]<\/code><\/pre>\r\n<\/blockquote>\r\n\r\n<h2>Understanding the Code<\/h2>\r\n<ul>\r\n<li><strong>RewriteEngine on<\/strong> \u2013 Enables URL rewriting.<\/li>\r\n<li><strong>RewriteCond %{HTTP_HOST} ^(www.)?yourdomain.com$<\/strong> \u2013 Ensures the rule applies only to your domain.<\/li>\r\n<li><strong>RewriteCond %{REQUEST_URI} !^\/newfolder\/<\/strong> \u2013 Prevents an infinite redirect loop.<\/li>\r\n<li><strong>RewriteCond %{REQUEST_FILENAME} !-f<\/strong> \u2013 Ensures the request is not for an existing file.<\/li>\r\n<li><strong>RewriteCond %{REQUEST_FILENAME} !-d<\/strong> \u2013 Ensures the request is not for an existing directory.<\/li>\r\n<li><strong>RewriteRule ^(.*)$ \/newfolder\/$1 [L]<\/strong> \u2013 Redirects all requests to the subdirectory.<\/li>\r\n<li><strong>RewriteRule ^(\/)?$ newfolder\/index.php [L]<\/strong> \u2013 Ensures the homepage loads correctly.<\/li>\r\n<\/ul>\r\n\r\n<h2>Steps to Apply the Redirect<\/h2>\r\n<ol>\r\n<li>Access your website files via <strong>FTP<\/strong> or <strong>cPanel File Manager<\/strong>.<\/li>\r\n<li>Locate the <code>.htaccess<\/code> file in your root directory.<\/li>\r\n<li>Edit the file and insert the provided code.<\/li>\r\n<li>Replace <code>yourdomain.com<\/code> with your actual domain.<\/li>\r\n<li>Replace <code>newfolder<\/code> with your actual subdirectory name.<\/li>\r\n<li>Save the changes and clear your browser cache.<\/li>\r\n<\/ol>\r\n\r\n<h2>Common Issues and Fixes<\/h2>\r\n<ul>\r\n<li><strong>Infinite Redirect Loop:<\/strong> Ensure the <code>RewriteCond %{REQUEST_URI} !^\/newfolder\/<\/code> condition is correctly placed.<\/li>\r\n<li><strong>500 Internal Server Error:<\/strong> Double-check for syntax errors in the <code>.htaccess<\/code> file.<\/li>\r\n<li><strong>Redirect Not Working:<\/strong> Make sure <strong>mod_rewrite<\/strong> is enabled on your Apache server.<\/li>\r\n<\/ul>\r\n\r\n<h2>Need Help?<\/h2>\r\n<p>If you&#8217;re experiencing issues with your redirect, UnderHost\u2019s support team is available 24\/7. Contact us via <a href=\"https:\/\/customerpanel.ca\/client\" target=\"_blank\">@CustomerPanel<\/a> for expert assistance.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Avoid redirect loops when pointing your domain to a subdirectory using a proper .htaccess configuration.<\/p>\n","protected":false},"author":1,"featured_media":5109,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,11],"tags":[],"class_list":["post-3688","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-controlpanel","category-how-to"],"_links":{"self":[{"href":"https:\/\/underhost.com\/blog\/wp-json\/wp\/v2\/posts\/3688","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/underhost.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/underhost.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/underhost.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/underhost.com\/blog\/wp-json\/wp\/v2\/comments?post=3688"}],"version-history":[{"count":2,"href":"https:\/\/underhost.com\/blog\/wp-json\/wp\/v2\/posts\/3688\/revisions"}],"predecessor-version":[{"id":5122,"href":"https:\/\/underhost.com\/blog\/wp-json\/wp\/v2\/posts\/3688\/revisions\/5122"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/underhost.com\/blog\/wp-json\/wp\/v2\/media\/5109"}],"wp:attachment":[{"href":"https:\/\/underhost.com\/blog\/wp-json\/wp\/v2\/media?parent=3688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/underhost.com\/blog\/wp-json\/wp\/v2\/categories?post=3688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/underhost.com\/blog\/wp-json\/wp\/v2\/tags?post=3688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}