Managing .htaccess Files
51 views
Website Management
<h3>Understanding .htaccess</h3>
<p>The <code>.htaccess</code> file is a powerful configuration file used by the Apache web server. It allows you to control URL redirects, password protection, caching, and many other features on a per-directory basis without modifying the main server configuration.</p>
<h3>Locating and Editing .htaccess</h3>
<ol>
<li>Open DirectAdmin File Manager.</li>
<li>Navigate to your <code>public_html</code> directory.</li>
<li>If you do not see a <code>.htaccess</code> file, ensure hidden files are visible (look for a <strong>Show Hidden Files</strong> option).</li>
<li>Click on <code>.htaccess</code> to edit it, or create a new file named <code>.htaccess</code> if one does not exist.</li>
</ol>
<div class="alert alert-warning">Always back up your existing <code>.htaccess</code> file before making changes. A syntax error in this file will cause a 500 Internal Server Error for your entire website.</div>
<h3>Common .htaccess Rules</h3>
<p><strong>Redirect HTTP to HTTPS:</strong></p>
<pre><code>RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]</code></pre>
<p><strong>Redirect www to non-www:</strong></p>
<pre><code>RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]</code></pre>
<p><strong>Password-Protect a Directory:</strong></p>
<pre><code>AuthType Basic
AuthName "Restricted Area"
AuthUserFile /home/username/.htpasswds/public_html/passwd
Require valid-user</code></pre>
<p><strong>Block an IP Address:</strong></p>
<pre><code>Deny from 192.168.1.100</code></pre>
<p><strong>Set Custom PHP Values:</strong></p>
<pre><code>php_value upload_max_filesize 64M
php_value post_max_size 64M
php_value max_execution_time 300</code></pre>
<h3>URL Rewriting</h3>
<p>The <code>mod_rewrite</code> module enables clean, SEO-friendly URLs:</p>
<pre><code>RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]</code></pre>
<div class="alert alert-info">Most CMS platforms like WordPress automatically create and manage their own <code>.htaccess</code> rules. If you are adding custom rules, place them above or below the CMS-generated block and do not modify the auto-generated sections.</div>
Need More Help?
Can't find what you're looking for? Our support team is ready to help.
Contact Support