Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make .htaccess RewriteCond check domain name?

Tags:

I have this rule:

RewriteRule ^(about|installation|mypages|privacy|terms)(/)*$     /index.php?kind=portal&id=1&page=$1&%{QUERY_STRING} [L] 

How can I change it so that it would work only for a specific domain, www.domain.com for example?

like image 701
D_R Avatar asked May 28 '12 13:05

D_R


People also ask

What is htaccess RewriteCond?

htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.

What is NC in RewriteCond?

The [NC] specifies that the http host is case insensitive. The escapes the "." - because this is a special character (normally, the dot (.) means that one character is unspecified). The final line describes the action that should be executed: RewriteRule ^(.*)$ http://www. example.com/$1 [L,R=301]

What is RewriteCond and RewriteRule?

RewriteRule is used to rewrite the url as the name signifies if all the conditions defined in RewriteCond are matching. One or more RewriteCond can precede a RewriteRule directive. If we talk about traditional programming RewriteCond works just like 'If' condition where you can use conditions like AND, OR, >=, == , !

What is mod_rewrite?

mod_rewrite is an Apache module that allows for server-side manipulation of requested URLs. mod_rewrite is an Apache module that allows for server-side manipulation of requested URLs. Incoming URLs are checked against a series of rules. The rules contain a regular expression to detect a particular pattern.


1 Answers

You need a rewrite condition:

RewriteCond %{HTTP_HOST} ^www.domain.com$ 

before your rewrite rule.

If you list several rewrite conditions before your rules, everyone of them must match for the RewriteRule to be executed, for example:

RewriteCond %{HTTP_HOST} ^www.domain.com$ RewriteCond %{HTTP_HOST} ^www.domain2.com$ 

which will of course NOT work, because the HTTP_HOST cannot contain simultaneously both values.

You must then use the [OR] modifier:

RewriteCond %{HTTP_HOST} ^www.domain.com$ [OR] RewriteCond %{HTTP_HOST} ^www.domain2.com$ 

so that the RewriteRule is executed if ANY of the above conditions match.

See http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritecond for more information.

like image 80
SirDarius Avatar answered Jan 01 '23 22:01

SirDarius