Category: htaccess

How to know whether mod-rewite is enabled on Apache server – enable mod-rewite

How to check whether mod-rewite is enabled on Apache server mod-rewite is a URL rewriting module provided by Apache. We use this module to rewite or beautify our site URLs. Although all web hosting service providers have pre-installed this module but in some cases you must enable it yourself in order to use RewriteRules in…
Read more

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

How to rewrite domainA to domainB using htaccess Apache Proxy Module and RewriteRule

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