Customizing .htaccess for SEO and Beyond: Rules and Tips
.htaccess is a file used by Apache servers to manage certain server settings. This file is located in the root directory of your website. It has a wide range of features, but in this article we will look at features that can improve your site's SEO and affect its promotion. The article itself is divided into 2 parts:
- Settings for all sites.
- Special settings
Functionality that will help to solve some specific tasks.
Important: Before making any changes to the .htaccess file, it's a good idea to back up the file. Incorrect configuration can lead to problems accessing the site.
Also, for .htaccess to work, mod_rewrite module must be enabled on your hosting.
Settings for all sites.
Search engines such as Google may interpret the same pages with different URLs as different pages on the site. This can lead to duplicate content, which in turn will reduce the ‘weight’ of your page and worsen its ranking in search.
Redirects to avoid duplicate content. To enable redirection, you need to add a directive to the .htaccess file:
RewriteEngine On
Redirect www.domain.com -> domain.com или наоборот.
#www.domain.com -> domain.com
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
#domain.com -> www.domain.com
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} (.+)$
RewriteRule ^(.*)$ https://www.%1/$1 [R=301,L] .
Redirect http://domain.com -> https://domain.com
ewriteCond %{HTTPS} !=on [OR]
RewriteCond %{SERVER_PORT} !^443
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
or
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Redirecting an address ending with a slash domain.com/// -> domain.com
RewriteCond %{THE_REQUEST} //
RewriteRule ^(.*)$ /$1 [R=301,L,NE]
Redirect domain.com/index.php -> domain.com
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.(php|html|htm)\ HTTP/
RewriteRule ^(.*)index\.(php|html|htm)$ $1 [R=301,L]
Caching and speed
It is also a good idea to enable content compression if it is supported by your server. This will reduce the amount of data transferred and result in faster page loading.
<ifModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/css text/javascript application/javascript application/x-javascript
</ifModule>
Another way to speed up site loading is to specify caching headers
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
Specifying the 404 error page. It is necessary if your CMS or framework does not have built-in processing mechanisms. To check this is quite simple, just try to open on your site knowingly not existing page. If it opens page 404 of your site means to add this directive in htaccess is not necessary, if you saw a browser error, then processing is missing or not configured. In order to fix it, you just need to add it to htaccess:
ErrorDocument 404 /404.html
Additional functionality
301 Redirections
This is probably the most common and important function of the .htaccess file for SEO, it tells search engines that a page has been permanently moved. This may be required if you have changed the url address of the page. Also 301 redirect will help you to replace your website domain with a new one without losing SEO position.
Redirect 301 /old-page.html /new-page.html
Blocking access to system files
It can also be a good idea to protect system files from being accessed from the internet.
<FilesMatch "(?i)((\.tpl|\.ini|\.log|(?:\.(?:bak|config|sql|fla|psd|ini|log|sh|inc|swp|dist|orig|save|txt))|(?:~)$))">
Order allow,deny
Deny from all
Satisfy All
</FilesMatch>
Restricting access to the site administration
Another good idea is to restrict access to the site's admin area only from certain IPs. Of course, only if you have the ability to provide static ip-addresses, for example, order them from your Internet Service Provider or use VPN with a static exit point. For example, your static address is 123.45.6.7, in this case you can add the following lines to your .htaccess file to the admin area of the site
<Directory "/manager">
Order Deny,Allow
Deny from all
Allow from 123.45.6.7
</Directory>
Conclusion
Using the .htaccess file allows you to control many aspects of the site, from simple redirects to complex security settings. With it, you can:Optimize SEO: Settings such as 301 redirects help avoid duplicate content and improve site indexing by search engines.
Improve performance: Gzip data compression and static caching speed up page loading.
Strengthen security: Restricting access to certain sections of the site by IP or blocking access to system files reduces the risk of hacking.
Flexibly manage content: The ability to redirect requests or change server behavior in response to certain conditions gives developers deep control over web resources.
As such, the .htaccess file is an important and handy tool that, when used correctly, can greatly improve the quality, security, and performance of a website.