Using .htaccess for Redirects and Canonical URLs
The .htaccess file sits in your site's root folder and lets you control redirects, URL rewriting and canonical URL rules on Apache/LiteSpeed servers — no code changes needed. Here's how to edit it safely on Hoststack and the rules most sites actually need.
Finding and editing .htaccess
- Open File Manager from cPanel/DirectAdmin and navigate to
public_html - Enable "Show Hidden Files" — .htaccess starts with a dot, so it's hidden by default
- If it doesn't exist, create a new file named exactly
.htaccess(no filename before the dot) - Always download a backup copy before editing — one wrong rule can 500-error your whole site
Forcing HTTPS (canonical protocol)
Add this to the top of .htaccess to redirect all http:// traffic to https://. Every Hoststack plan includes free SSL, so this is safe to add as soon as your certificate is active:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Choosing www or non-www as canonical
Google treats www.yoursite.com and yoursite.com as separate URLs. Pick one as canonical and 301-redirect the other, so search engines and links consolidate to a single version. To force www:
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [L,R=301]
Common redirect rules
- Single page:
Redirect 301 /old-page.html /new-page - Whole domain:
Redirect 301 / https://newdomain.com/ - Remove trailing slashes: use
RewriteCond %{REQUEST_URI} (.+)/$with a rule stripping the final/ - Use 301 for permanent moves (passes SEO value) and 302 only for temporary redirects
Avoiding a 500 error
If your site goes down right after an edit, it's almost always a syntax mistake in .htaccess. Reopen the file, remove the last rule you added, and reload the site. WordPress users should never touch the block between # BEGIN WordPress and # END WordPress — add custom rules above or below it instead.
Locked yourself out with a bad rule?
Our support team can restore your .htaccess from backup or review your rules before you publish.
Contact Support