Tag: htaccess

Deny access to multiple ip addresses using RewriteRule in htaccess

With a RewriteRule directive you can deny access to a specific IP address or multiple ip addresses at ones. If you need to deny access to a visitor IP address you can use a RewriteRule directive in htaccess file that checks the visitor IP address and blocks access to that address. Blocking IP addressess using…
Read more

How to redirect a URL with Query String apache htaccess

In this post we will learn how to redirect a URL with query string using RewriteRule directive in htaccess. Query string represents URL part that is after ? character in URL. A URL with query string looks like the following example URL: http://example.com/file.php?q=foo&lang=en As you can see the above example URL contains a querystring q=foo&lang=en…
Read more

Enforce https and www in a single htaccess rule

It is quite possible to redirect http and non www requests to https and www with one single redirect using a RewriteRule in htaccess. Assuming, you want your site visitors to always redirect to https://www version of your site regardless of they come by http or non-www you can use a single redirect Rule that…
Read more

Set env variable in htaccess – Create Apache Environment variable via htaccess

You can set an environment variable using your htaccess file. There are two ways to do this via an htaccess. RewriteRule SetEnv directive Setting env variable via RewriteRule To set env variable through a RewriteRule, you need to use E flag. Syntex for the E flag is as follows E=variable:value . The following is basic…
Read more

htaccess domain redirection www to non-www and the other way around code examples

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…
Read more

Commonly used regular expression patterns in apache 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…
Read more

How to redirect non-logged in users using RewriteRule in 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…
Read more

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

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…
Read more

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…
Read more

How to set cookies using RewriteRule in htaccess or server.config file?

Hello! In this post I will show you how we set an http cookie using a RewriteRule directive in an htaccess or server.config file. I saw many unanswered and confusing posts on StackOverflow and on other sites so I thought It would be worth writing a post that can help peope who are searching the…
Read more