Skip to content Skip to footer

WordPress htaccess

WordPress uses htaccess to do redirtects to the appropriate wordpress folder.

This is a nice feature, as it prevents unauthorized access but it also prevents non-wordpress content from coexisting with wordpress. So if you have a mini site, wordpress auto redirects it to its folder.

You can change this behaviour by changing the .htacces file. This file is typically located in the root folder where your website is hosted so public_html or /var/www/html.

There might be additional configuration but typically it would be something like below with the # Begin WordPress and #End wordpress directives

The addiotional configurations need to be inserted after the RewriteEngine On(line 6 highlighted below) directive and before the remainder of the wordpress configurations

				
					# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^wp/
RewriteRule .? wp%{REQUEST_URI} [R=301,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

</IfModule>

# END WordPress
				
			

Let’s say your non-wordpress content is in a folder called portfolio and you wish to make it accessible.

So what you need to do is to create htaccess 

  •  Matching condition where the URLS have portfolio in the path
  • Rewrite rule that essentially does nothing by rewriting rule to itself

The directive should be as below

				
					RewriteCond %{REQUEST_URI} ^/portfolio/(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^/portfolio
RewriteRule ^.*$ - [L]
				
			

There are many mod_rewrite directives and covering what they all mean is beyond the scope of this post, but essentially each request is compared to the conditions(RewriteCond directives in line 1-2  above) and if they match the conditions they are rewritten following the RewriteRule in line 3. 

The first rule tries to match URI’s where the path matches the portfolio directory with the files.

So this matches https://DOMAIN/portfolio/index.php, https://DOMAIN/portfolio/index.html etc.

The second rule matches is used to match the instance where the user did not put in the trailing folder forward slash. So the rule will match https://DOMAIN/portfolio .

The RewriteRule defines where the URL requested will be rewritten essentially ever URL that writes to the same so there is no rewriting for requests in portfolio.

[L] indicates this is the last rule and stop processing.

This configuration will thus redirect requests into the wordpress site and allows access to the other subfolders functioning as minisites outside of wordpress.

 

Leave a comment