Categories
apache Apache mod-rewrite and htaccess related Articles htaccess mod-rewrite

How to rewrite domainA to domainB using htaccess Apache Proxy P flag

Mod proxy is a powerful Apache modules and provides URL rewriting and domain mapping capabilities. With proxy module enabled on apache server you can rewrite your domain URLs to an external domain that is being hosted on a diffrent web server. Proxy module allows you to use P flag (Proxy) in RewriteRule directive to rewrite URLs from one server to the other.

References :

How do you enable proxy module?

In order to use mod-Proxy,you need to enable proxy module on your apache server. To enable the module type the following commands in your terminal window :

  • Install mod proxy using the following command
    sudo a2enmod proxy
  • Restart your server

    sudo /etc/init.d/apache2 restart

    or

    sudo /etc/init.d/apache2 restart

After enabling proxy module ,you can use mod rewrite [P] flag in your htaccess file to map your domain.
The following is a basic mod rewrite example to rewrite Domain A to Domain B.

RewriteEngine On
RewriteRule (.*) http://example2.com/$1 [P]

The rewrite rule example above maps a domain to another domain using the P (Proxy module) . Any request to domain A will be served by Domain B without changing the URL in browser address bar.

ProxyPass directive

ProxyPass is a directive of proxy module and can be used in server.config file to proxy requests between two domains. ProxyPass is faster then using P flag in a RewriteRule. The following example rewrites DomainA/foo to DomainB/bar . You will get the contents from domainB by going to domainA URL in browser.

ProxyPass “/foo” “http://domainB.com/bar”

You can also use ProxyPass directive inside Location directive to map a specific dirctory Path

<Location “/thisDir/foo”>
ProxyPass “http://domainB.com/bar”
</Location>

ProxyPass insde Location directive will only apply to the path specified in Location. If The example code above is placed in server.config file in domainA this will rewrite domainA.com/thisDir/foo to domainB.com/bar .
You can also use a regular expressions based pattern in ProxyPassMatch directive.

ProxyPassMatch “^/foo$” “http://domainB.com/bar”

Using proxy module in htaccess RewriteRule

If you don’t have access to your main server.config file then you can use RewriteRule in your htaccess. P flag is used to map requests between two domains.

RewriteEngine on
RewriteRule ^foo$ https://domainB.com/bar [P]

This will internally map /foo to /domainB.com/bar just like a normal rewrite rule you use to rewrite your domain URLs.

Categories
Apache mod-rewrite and htaccess related Articles

How to redirect a www subdomain to non-www with htaccess

htaccess enforce www or non-www

The search ranking of your domain can go down If your website is accessible with and without www as both URLs with same contents are indexed by search engines and can cause duplicate contents issues.
In this case, all you need to do is either redirect your www subdomain to non-www (naked domain) or the other way around.
You can easily redirect your domain using a RewriteRule in htaccess or server.config file.
In this post I will show you how to redirect a domain using htaccess. You can simply copy and paste the code to your htaccess file from this post.

Table of contents

non-www domain to www

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) http://www.example.com%{REQUEST_URI} [L,R=301]

Explanations : The first line RewriteEngine on tells server to turn on the engine for rewriting URLs.
The second line is a condition for the rule RewriteCond %{HTTP_HOST} ^example.com$ [NC] here we match against the host header using “example.com” string. The NC flag makes the host match case-insenstive .
The third line RewriteRule (.*) http://www.example.com%{REQUEST_URI} [L,R=301] redirect all urls to www version of the domain.
The rule is triggered only if the condition is met meaning that if the host header is example.com .

non-www to www generic rule

If you have more then one domain to redirect from non-www format to www you can use a generic rule that checks http_host header dynamically. You can use a regex based pattern something like the following:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^((?!www\.).+)$ [NC]
RewriteRule (.*) http://%1%{REQUEST_URI} [L,R=301]

The rule above will redirect all non-www hosts to www.
The %1 variable contains the host value captured in the RewriteCond regex. The rule will also enforce www on subdomains, if you do not want your subdomain to redirect to www , put an additional condition above the RewriteCond ie : RewriteCond %{HTTP_HOST} !^foo.example.com$ [NC] excludes foo.example.com subdomain from the rule. You can add as many conditions as you want but put them just before the main RewriteCond.

www to non-www

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule (.*) http://example.com%{REQUEST_URI} [L,R=301]

With the above rule your www subdomain will redirect to non-www root domain.

www to non-www generic rule

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

Categories
apache Apache mod-rewrite and htaccess related Articles htaccess mod-rewrite

Commonly used regular expression patterns in apache RewriteRule

Regex in RewriteRule
RewriteRule directive uses a Regular expression based pattern to match against URL path. In this post we will cover some basic and commonly used patterns that are most often used in RewriteRule.

RewriteRule basic regex

  • (.*) Catch-all and save (zero or more characters)
  • (.+) Catch-all and save (one or more characters)
  • ([a-zA-Z0-9]+) Catch-all alpha numbric Chars (A to Z , a-z and 0-9)
  • ([0-9]+) Catch-all numbric Chars in URI ( 0-9)
  • ([a-z]+) Catch-all lower case URI chars (a to z)

(.*) matches all characters in URI and saves the value in $1 variable which can be used in rule’s substitution string.

RewriteRule (.*) /file.php/$1 [R]

The rewrite rule above will redirect any URI to /file.php/anyURi . The pattern (.*) matches all URL paths including / (hompage). The $n variable holds whatever (.*) captures.

(.+) matches one or more characters in an URI string and saves the matched value in $1 variable.

RewriteRule (.+) /file.php/$1 [R]

The rule above will redirect /anyURI to /file.php/anyURI . The difference between + and * in the pattern is that + means to match one or more time . The minimum required character in URI is one while the * matches zero or more time. The pattern (.+) will not match your hompage “/” as the single “/” is treated as no char in RewriteRule.

([a-zA-Z0-9]+) matches alphabetical characters in URI. Only the characters you put inside [] are matched. A-Z matches UPPER CASE characters ranging from A-Z .

RewriteRule ([a-zA-Z0-9]+) /file.php/$1 [R]

Our RewriteRule above will match all uri of the following formats /thisuri , /ThisURI123 etc..

([0-9]+) matches a numbric URI string ie “/123” .

RewriteRule ^([0-9]+)$ /file.php/$1 [R]

The rule above will redirect example.com/123 to example.com/file.php/123 . The ^ indicates start of the URI string and $ indicates end of the string . If the ^ and $ are removed in the rule above then the rule will also redirect example.com/foo123 but the pattern will only capture numberic part of the uri “123”

^([a-z]+)$ matches lower case alphabetical URI characters ie. ” example.com/thisuri”

RewriteRule ^([a-z]+)$ /file.php/$1 [R]

I hope it was helpful. Thanks for reading.

Categories
apache htaccess mod-rewrite

How to redirect non-logged in users using RewriteRule in htaccess

Redirect based on cookie value htaccess

Redirect non-logged in users using htaccess

With apache mod-rewrite it is quite possible to manipulate http cookies. The cookie you set using php or RewriteRule can easily be accessed using RewriteCond directive.
If you need to redirect your non-logged in users of your website to a diffrent page using RewriteRule you need to check the COOKIES in RewriteCond directive .
You can use %{HTTP_COOKIE} variable which is a pre defined mod-rewrite variable to check if the user visiting your site has cookies set.
Assuming the following user cookie is set user=loggedin
You can simply use a RewriteRule to redirect your non-logged in users to a diffrent page based on if the cookie exists.

RewriteEngine on
RewriteCond %{HTTP_COOKIE} !^user=loggedin$ [NC]
RewriteRule ^ /page2.php [L,R]

The rewrite rule above will redirect user to /page2.php if the cookie user=loggedin doesnt exist or isnt set. The rule also redirects files like css , js and other website resources. You can simply add other conditions to exclude your existent files so that it can only redirect the user request. The following rule should work in that case :

RewriteEngine on
#exclude files
RewriteCond %{REQUEST_FILENAME} !-f
#exclude dirs
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{HTTP_COOKIE} !^user=loggedin$ [NC]
RewriteRule ^ /page2.php [L,R]

Categories
apache htaccess mod-rewrite

How to use mod rewrite %{THE_REQUEST} variable in htaccess?

How to use THE_REQUEST

Many users who use htaccess often get confused by mod-rewrite %{THE_REQUEST} variable. The questions what is it and how do we use it appear on their mind. In this post I will explain what %{THE_REQUEST} actually is and how we can use it in rewriteRule.

What is %{THE_REQUEST} in mod rewrite?

%{THE_REQUEST} is a mod rewrite per defined variable like other variable ie %{HTTP_HOST} . To understand what URI string the_request variable actually contains you can write a rule to print it.
THE_REQUEST variable contains the full URI string including Request method and Server protocol. The following is the example of THE_REQUEST string . When you type /file.php in your browser , your browser sends the following request to server:

GET /file.php HTTP/1.1

  • GET is the request method
  • /file.php is the file you requested.
  • HTTP/1.1 is the protocol version of your apache server.

We use %{THE_REQUEST} in RewriteCond to conditionally rewrite urls and avoid infinite loop error as this variable works as an anti loop protection for rewriteRules.

RewriteEngine On
RewriteCond %{THE_REQUEST} GETs/file.phpsHTTP1.1 [NC]
RewriteRule (.*) /file [R]

In the example above we match against %{THE_REQUEST} using regex. If the regex pattern matches then the rule is triggered. The rule will redirect /file.php to /file. You can also use a simple regex pattern that only matches the file name

RewriteEngine On
RewriteCond %{THE_REQUEST} file.php [NC]
RewriteRule (.*) /file [R]

THE_REQUEST variable is often used with rules that remove file extensions. When two rules (Rule with R flag and Rule with L) rewrite the same file name THE_THE_REQUEST should be used at the top of External rewrite rule (Rule with R flag) to avoid the loop error.

Categories
Apache mod-rewrite and htaccess related Articles htaccess mod-rewrite

Remove multiple file extensions using RewriteRule in htaccess.

Remove html PHP extension using htaccess

How to remove multiple file extensions using RewriteRule in htaccess ?

.htaccess is a directory level configuration file used on apache server. Many users who don’t have access to their main server.config file use htaccess to rewrite URLs and configure other custom server settings. Htaccess is often used by Apache users to shorten URLs when they want to turn a long and non-memorable URL into short and memorable one. One of the most used rewriteRule on htaccess is “File extension removal” rule. Users find it easier then server.config to RewriteRule through htaccess. In this post I will show you how you can remove any file extension using htaccess.

What you will need:

  • Apache server
  • Mod-rewrite
  • .htaccess support

Apache server

You must have an apache server to use RewriteRules.

Mod-rewrite

Mod-rewrite is a rewrite module provided by apache. You must have this module enabled on your server in order to use RewriteRules directive. Some hosting service providers have enabled it by default but in case its not enabled ,you can enable it by adding one mazic line AllowOverride All in your server.config file. If you don’t have access to this file then ask your hosting service to enable it for you.

.htaccess

.htaccess is where you will write rule code. htaccess is a dot perfix file. If your hosting service hasn’t enabled it then ask them to enable it.

Remove file extension using RewriteRule

With a RewriteRule directive, you can remove the traling .php from your php files. For example You will only type /filename instead of /filename.php . Filenames with extension do not cause any SEO issues but if you remove the extension then appearance of URL becomes awasome. The following is the rule to remove .php extension from urls. You can edit it to remove other extensions accordingly by just replacing .php with your file extension :

RewriteEngine on
#2)
RewriteRule %{THE_REQUEST} /(.*).php [NC]
#2)
RewriteRule (.*) /%1 [L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*)/?$ /$1.php [L]

The rewrite rule above would redirect any php file url ie (foo.php) to remove the extension. The first set of rule redirects /file.php to /file and the second one then internally maps /file to its original location /file.php .

Remove multiple file extensions using RewriteRule in htaccess

It is possible to remove multiple file extensions using two rules. You can capture file extensions using regex in RewriteCond and then rewrite the request accordingly.

RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+).(php|html|css) [NC]
RewriteRule ^ /%1 [L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*)/?$ /$1.php [NC,L]

The simple approach works for any file extension you want to remove.

Categories
apache htaccess mod-rewrite

Diffrence between URL rewriting and URL redirection htaccess topic

URL rewriting and URL redirection are two different terms used to map a URL from one location to the other. These two terms often confuse new users especially when they refer to URL related queries. Some users even intermix the URL rewriting logic with URL redirection like they are talking about a particular thing with two different languages. I often see such questions on StackOverflow and encourage posters to fix it.

In this post I will give you a clear overview of what these two terms actually mean and how we use them with URLs.

URL redirection

URL redirection is when an URL redirects from one location to the other and the appriance of URL changes in browser address bar from old to new (from the typed URL to the new location). URL redirction happens client side . Server gives a command to your web browser to change the URL to a new location. Lets understand with a real world example : if you click on this link my server will send a redirect command to your browser to go from this page to example.com.

Notice how the URL changes in your browser address bar to example.com. This redirection is also called external redirection or transpirant redirection as its visible to your web browser and your browser knows whats happening on the screen. On apache server you can create a Redirect either by using a file based method in html, PHP or using Redirect commands in htaccess or server.config file. Apache server provides multiple methods to redirect URLs by htaccess. There are 2 diffrent modules that you can use for URL redirection on apache Mod-alias and mod-rewrite both modules support htaccess and server.config context.

4 ways to redirect URLs on apache server

Given bellow are the common methods to redirect URLs from a website.

  1. HTML:
    HTML anchor tag (a) is used to redirect the currunt page URL to a specific location
    <a href=”http://example.com”&#62redirect to example.com”</a>

  2. PHP:
    Redirecting the current page to example.com
    <?php
    header(“location:https://example.com/”);
    ?>

  3. Redirecting a specific path to example.com using Redirect directive in htaccess:
    Redirect /path http://example.com/

  4. Redirecting a specific path to example.com using RewriteRule in htaccess:

    RewriteEngine on
    RewriteRule ^thispath http://example.com [R]

URL rewriting

URL rewriting is way of rewriting urls. URL rewriting is When a URL path internally maps to another path on server. By “internally” I mean an “Invisible URL redirection” . You and your browser can’t see how the url changes from Old to new as it starts and ends behind the screen and your browser stays on the same URL. URL rewriting is server side only as it happens on a webserver without informing the client of the redirection. You get the web page from new location on the same (typed) url as it doesn’t change from old to the new one.
URL rewriting is also called internal URL or invisible url redirection as its invisible to a web browser only the webserver executes this redirection and sends the contents of the new page to browser. For example the following url http://example.com/file can be rewritten to http://example.com/file.php by using a RewriteRule directive on apache server. And then instead of typing /file.php you can simply type the new url /file your server will internally redirect/rewrite it to /file.php serving you the contents of /file.php on the /file URI.

Rewriting URLs on apache server

Apache server provides mod-rewrite module for rewriting URLs. In order to use this module you must first enable it on your server if it isnt already enabled.
There are hundreds of URLs rewriting basic examples on Apache guide if you are new you can read their complete guide to understand how it works . To rewrite URLs on apache server, you can either use your config file or your htaccess to write rules.
URL rewriting is done by using RewriteRule directive of mod-rewrite module. The following is a basic example of how we use RewriteRule in an htaccess file. The rule rewrites /a.php to /b.php .

RewriteEngine on
RewriteRule ^a.php$ /b.php [L]

Categories
apache htaccess mod-rewrite

How to set cookies using RewriteRule in htaccess file?

How to set cookies in htaccess

In this post I will show you how we can set an http cookie using a RewriteRule directive in an htaccess or server.config file.
I saw many unanswered and some wrong answered posts on StackOverflow and on other sites so I thought It would be worth writing a post here that can help peope looking for help regarding Htaccess and mod_rewrite cookies.

Setting cookies using mod_rewrite

With RewriteRule directive you can set http cookies the same way you use the directive for rewriting URLs. Cookies are set using [CO] Cookie flag of apache mod-rewrite. The cookies you set via RewriteRule can also be accessed by client or server scripting languages like PHP and JS.

Mod rewrite cookies are mainy used for rewriting requests based on http cookie header.
You often set a mod rewrite cookie to rewrite/direct URLs if a specific cookie exists or based on cookie value .

Let’s start with a basic example first:

RewriteEngine On
RewriteRule (.*) - [CO=user:john]

The rewrite rule above will set cookie named user and as you can see we manually assigned value john to it.
Explanation :

  • The first line “RewriteEngine on” tells server to turn on the engine for rewriting URLs. This setting is disabled by default so you must use this line ones at the top of RewriteRules in htaccess or server.config files.
  • The second line is that does the Rewrite mazic and sets the cookie whenever you request an URI from server.
    RewriteRule (.*) – [CO=user:john]” matches any incoming URIs as the pattern (.*) means to match anything. The hyphen in the Rule’s destination above lets the URI pass through unchanged meaning that if the request is for /file.php only the CO flag will be applied to the URL and there will not be any change in the URL. In most cases when setting cookies by RewriteRule you use as the destination path because we don’t want to rewrite the urls we just want to set a cookie for URLs.
    [CO=user:john]” sets user cookie with value john. You can also use a dynamic value using regex match “$1” or “%1” .

Setting cookies for specific URIs with mod_rewrite

In the basic example above we learned how to set cookies when any URL passes through RewriteRule. You can also set cookies for a specific URI. For example to set cookies only for a single path or file ie /this-file.php you can restrict the pattern of RewriteRule to match only this URI. You just need to write a regular expression pattern to match that particular path.

RewriteEngine On
RewriteRule ^this-file.php$ - [CO=user:john]

This will set the cookie only when you request /this-file.php . This rule example might fail on server.config, add a leading slash in the pattern ie: ^/this-file.php$ to use this in a server.config file.

We use RewriteCond directive to check whether a cookie is set or notset and to test the value of a cookie that is already set. We can also check what value the cookie holds. The following is a basic example of checking whether a cookie is set :

RewriteCond %{HTTP_COOKIE} !^$

In the example above we want to make sure our cookie is set or not empty (!^$) . You can use a RewriteRule bellow this condition so it executes if the condition is met.

In the example bellow, we will redirect /this-file.php to root / if our user cookie is not set.

RewriteEngine on
RewriteCond %{HTTP_COOKIE} !user
RewriteRule ^this-file.php$ / [R,L]

RewriteEngine on
RewriteCond %{HTTP_COOKIE} ^user=john
RewriteRule ^this-file.php$ / [R,L]

The rule above will redirect /this-file.php to / if the cookie user and value john is set.

You can set a specific cookies if it doesn’t exist using the following RewriteRule:

RewriteEngine on
RewriteCond %{HTTP_COOKIE} !^user=john
RewriteRule .* - [CO=user:john,L]

In the example above we first check the HTTP_COOKIE header to ensure that the cookie “user=john” is notset and then we set it via RewriteRule.

By default, mod-rewrite cookies are set for all domains that point to the same document root. You can add an optional perameter domain to the CO flag. To set cookies only for a specific host ie:www.example.com you will use something like the following:

RewriteEngine on
RewriteCond %{HTTP_COOKIE} !^user=john
RewriteRule ^this-file.php$ - [CO=user:john:www.example.com,L]

By default mod-rewrite cookies are set for current session only which means when the browser window is closed the cookies also get destroyed. You can add a lifetime perameter in minutes to CO flag to customise cookies validity.

RewriteEngine on
RewriteRule ^this-file.php$ - [CO=user:john:www.example.com:10,L]

In the example above the user cookie is available only on www.example.com host and it expires after 10 minutes.

Cookies for specific path only

You can make the cookies available for a specific path only. By default mod-rewrite cookies are set for / which means the entire site. To set a specific path you may use :

RewriteEngine on
RewriteRule ^this-file.php$ - [CO=user:john:www.example.com,L]

Setting cookies expire time

You can set how long an http cookie will survive, by default mod-rewrite cookies are set for current browser session only and deleted when we close the browser window. You can add a time perameter to CO flag to extend the cookie validity . Time value is set in minutes.

RewriteEngine on
RewriteRule ^this-file.php$ - [CO=user:john:www.example.com:10,L]

This will set user cookie on www.example.com host with a validity of 10 minutes.

Categories
Apache mod-rewrite and htaccess related Articles htaccess moder rewrite rules

5 awesome htaccess copy and paste Rewrite rules with explanations

htaccess RewriteRules

I often see people struggling with their mod-rewrite code and when they think they can’t fix the issue themselves, they either go to stackoverflow or keep scrolling the search results and hopefully ,perhaps get the solution there.

URL rewriting is not as complicated as new users find it. Like other languages, URL rewriting is a mazic of syntex. If you are learning from the scratch then learning its basic syntex first can help you better understand it and speed up your learning skill.

There are alot of learning resources on the Internet including the official mod-rewrite guide where you can start.

In this post, I am going to share 5 apache RewriteRule scripts that you can easily copy and paste to your server.config or htaccess
file to use them.

#1) Rewriting a long url to make it short and search engine friendly

With apache mod-rewrite, you can turn a longer looking URL into short one.
Let’s assume you have long URL with query variables which looks something like the following:
http://example.com/users.php?user=foo

The one main problem with the URL above is that it is longer, not memorable and non-SEF . Search engines often ignore such URLs due to the duplicate URL issues.

To make the above URL short and SEF , you can use RewriteRule in your server.config or htaccess file.
With a RewriteRule you can turn the URL into the following format:

http://example.com/users/foo

The URL looks awasome now. Doesn’t it?

RewriteRule script :
RewriteRule ^users/(.+)$ /users.php?user=$1 [L]

You can simply copy and paste the rule above to your htaccess or server.config file and put it bellow “RewriteEngine On” .

#2 Setting an environment variable using RewriteRule

You can set a URI based environment variable on your server using RewriteRule directive. Settings the variable via RewriteRule is easier then setting it in php. You can set an environment variable for specific URIs when these URIs pass through the RewriteRule.
The following RewriteRule code example sets an environment variable named foo and value bar for file.php meaning that when you request the /file.php the rule will automatically set the variable. You can copy the code from textarea bellow and put it in your htaccess file.

RewriteRule script :
RewriteRule ^file.php$ - [E=foo:bar]

#3) Access files without extensions

A simple one liner code to ignore file extensions on Apache server. If you want to access your files without typing extensions at the end, for example to
access file.html as file you can use the following line in your htaccess file :

Options +Multiviews

#4) non-www to www URL redirection

RewriteRules can be used to redirect a non-www (naked domain) to its www version. To redirect your non-www URLs to www , you can use the rule bellow:
RewriteEngine on
RewriteCond %{HTTP_HOST} !www. [NC]
RewriteRule (.*) http://www.example.com%{REQUEST_URI} [R=301,L]

We use RewriteCond (condition) directive to test the host header string. If the test string %{HTTP_HOST} matches the pattern !^www then the rule is applied.. In simple words, if you type yourdomain.com into your browser the rule will redirect it to www.example.com .

#5) Custom 404 rule

With mod rewrite you can create a custom 404 rule for your website. You can redirect your visitors to a specific page on your site when they request a broken or not-found link.

The following RewriteRule script rewrites 404 requests to /file.php on your server. If you are using wordpress, add this before the WordPress default rule .

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) http://www.example.com%{REQUEST_URI} [R=301,L]

Categories
apache Seo

Should you htaccess or server.config file on apache server?

htaccess vs server.config . Which one should be used

Should you use htaccess or server.config file?

This is a common question among many apache mod-rewrite users especially among newbies as they are curious about it.
If you search this topic on google you will get hundreds of top results where experts advice you not to use htaccess.
One true answer to the question can be found on official apache mod-rewrite manual https://httpd.apache.org/docs/2.4/howto/htaccess.html .

Screenshot from the official apache mod-rewrite manual:

htaccess vs server.config file apache

What is a server.config file?

It is a server level config file located in your httpd server root. You can put directives in this file to customize the behaviour of your server.

What is an htaccess file?

htaccess is a directory leve config file located in your document root.
By directory level I mean to say that you can use it in your main directory if you want the htaccess to effect the folder and its subfolders or you can also use it in a particular folder/subfolder so that it can affect the folder accordingly.

Not all hosting providers allow you to access server.config

Some paid or free webhosting providers do not allow you to edit server.config on their server that means that you only have access to a dynamic configuration file called .htaccess.
htaccess file can be placed anywhere in any directory .
Even wordpress installation contains an htaccess file to create short and friendly URLs.
But the question “Should I use htaccess or server.config file ” still keeps popping up in our mind and everyone seems to be curious to know it.

server.config or htaccess?

If you do not have access to the main config file on your server then You can use htaccess. There is nothing wrong with using an htaccess file. You won’t lose your seo rank. But there are some performance issues that can effect your site.
Apache mod rewrite documentation says that “htaccess file slows down the performance of a webserver” . Yes it does..but very little 0.5 or 1%. You can’t even notice how much your server performance and page load speed is affected.
So you don’t have be worried about it.
The difference between a server.config and an htaccess file is that you a server.config file supports all directives while an htaccess doesn’t like RewriteMap and there are some other useful directives that you cant use in an htaccess file.

Another big diffrence between these contexts is how a web server reads/executives these files. A server.config file is read by server only once. A htaccess file is read on each request. When you request sa file or directory from webserver , your webserver first reads the htaccess to serve you the file dir according to commands you put in htaccess. Server.config file on the on other is read only onec you update it.

To better understand how an htaccess file can slow down your webserver, consider the following example :

Say you an htaccess file in your directory to rewrite “/file.php” to “/file2.php” .

You type “/file.php” in your browser address bar.

You webserver receive the request “/file.php” .
In order to serve you the “/file2.php” your server reads your htaccess two times.
1) Your server reads your htaccess contents and rewrites “/file.php” to “/file2.php” .
2) Your server again reads your htaccess file to serve you the destination file “/file2.php” .
As you can see your webserver read your htaccess two times to serve a simple request. Then obviously your page load speed will degrees by 1 or 2% .