Using .htaccess for SEO - Redirects, URLs, Content Browsing

Using .htaccess for SEO - Redirects, URLs, Content Browsing

It is no wonder that one of the most important tasks of search engine optimization (SEO) is to make your website search engine friendly. The competition in search engine results is high, so you should not ignore any advantages. Htaccess is one of your important files that allows you to control the functionality of your website and how it interacts with servers and browsers. We need to customize our htaccess files not only for security and control purposes, but also to maximize search engine optimization. Configuring Htaccess for SEO has features you need to enable or disable based on your needs.

Customized for better search engine optimization results .htaccess will cover every feature of SEO. We will also talk about the .htaccess file snippets that enhance the functionality of htaccess and how regular expressions are used in htaccess. First let's get to know this file, what is htaccess?

What is Htaccess?

Especially beginners may need a better understanding of what htaccess is. It is slightly different from your other files in the directory; extension without a name? Htaccess is a file that is not a file extension.

Technically, htaccess is used to configure each directory by web servers supported by Apache. Also, if your hosting company doesn't allow you to edit the .htaccess file, you can easily create a new one and override the parameters in your default .htaccess file. Directives can override certain features at the directory and subdirectories level, so uploading a .htaccess file to the root folder of your web server means that the directives apply to your entire website.

The file is easy to access. All you need to do is look for the file in the root directory. Most popular content management systems provide access to files from their own management panels. You can also access the file via FTP.


The directives give you more control over the directories you want to configure. These are the commands in the configuration file. Uses htaccess variants. With these variants, you can password protect files, control scanning, allow or prohibit IP addresses.

What does Htaccess do in SEO?

Search engine crawlers are interested in how you control the functionality of the website on .htaccess, and you can get positive points because the right configuration means reliability.

With Htaccess you can create clean URLs that search engines love.
You can solve 404, https errors and check your 301 redirects.
You can also use users who use an IP address or domain to block illegal search engine bots.

Editing .htaccess is not difficult. Improve the functionality and features of your site, but incorrect configuration will give your site errors. You may receive 500 internal server errors. To avoid such situations, you should back up your .htaccess file before making any changes. This way, your original file is safe and you can experiment.

Where is Htaccess file?

Depending on which platform you use, the .htaccess file is often located at the root of your directory. For example, if you use WordPress, you'll find it in the main directory of the WordPress installation. To access the .htaccess file, you must turn on the Göster Show Hidden Files ”feature.

SEO Friendly URLs

URLs play an important role in the ranking of search engines. According to Matt Cutts of Google, the structure of URLs plays an important role in ranking and adds value to the ranking factor of keywords in URLs. Another feature necessary for URLs to be search engine friendly is that they are as short as possible. URLs are both short and predictable of how they will lead to a page when clicked, which will signal search engines that you are interested in your users.

General URL Optimization

Let's start with a simple example, with the following statement you can change the entire URL structure on your website.

RewriteEngine On
Rewrite Rule ^test/ (a[a-zA-Z0-9]+)$ index.php?topic=$1

# Yukarıdaki ifade ile web sitenizdeki tüm URL'ler
#  biçimine dönüşür.

Remove file extensions from a URL (.php, .html)

With Htaccess you can also get rid of page extensions such as .html and .php. These extensions do not add value to the usage. To remove the extensions, simply copy and paste the following code into the .htaccess file.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f 
#PHP Uzantısını siler

RewriteCond %{REQUEST_FILENAME}.html -f 
Rewrite Rule ^(.*)$ $1.html
#HTML uzantısını siler

Kanonik robots.txt

With canonical redirect, you can improve robots.txt scanning. In general, the robot.txt file is located in the root directory, but bad bots or other malicious scripts can exhaust your resources by scanning the entire website to find your robots.txt file.

You can use .htaccess to find bots and other browser elements in the Robot.txt file. You can also prevent continuous requests for access to the “robot.txt” file. By doing this, you can reduce the load on the server and simplify the work of important browsers such as GoogleBot.

Robots.txt canonical redirect

<IfModule mod_rewrite.c> 
RewriteBase / 
RewriteCond% {REQUEST_URI}! ^ / Robots.txt $ [NC] 
RewriteCond% {REQUEST_URI} robots \ .txt [NC] 
RewriteRule % {REQUEST_URI} robots \ .txt [NC] RewriteRule. * [ R = 301, L] 

In the above code, all you have to do is change your “website ine to the URL of your site. The URL must be the public directory of your website. In the above method, we used Apache's rewrite module. However, if you are looking for an alternative solution, you can use mod_alias .

RedirectMatch 301 ^/(.*)/robots\.txt

URL www / http version redirect

One of the most basic problems is www, non-www redirects. With this redirect, you won't have to deal with URL duplication in the Google Search Console. You can solve the problem with a simple canonical label.


// HTTP yönlendirmenin bir benzeri
// Linke tıklama
window.location.href = "";


RewriteEngine On
RewriteBase /
Rewritecond %{HTTP_HOST} ^www\.site\.com$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

You can try to use possible variations in the .htaccess file when you force it to www or ssl, for example:

RewriteEngine On
RewriteBase /

Force WWW

RewriteCond %{HTTP_HOST} ^site\.com
RewriteRule (.*)$1 [R=301,L]

Force SSL

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$$1 [R,L]

IP Blocking

Order Deny,Allow
Deny from
Deny from

Apache 2.4 Updated Version

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

PHP Method

$protocol = (@$_SERVER["HTTPS"]    == "on") ? "https://" : "http://";

if (substr($_SERVER['HTTP_HOST'], 0, 4) !== 'www.') {
    header('Location: '.$protocol.'www.'.$_SERVER    ['HTTP_HOST'].'/'.$_SERVER['REQUEST_URI']);


    type: "POST",
    url: reqUrl,
    data: reqBody,
    dataType: "json",
    success: function(data, textStatus) {
        if (data.redirect) {
            window.location.href = data.redirect;
        else {

Share Tweet Send