IVIR3zaM
IVIR3zaM

Reputation: 557

apache htaccess using conditions

I wanna use conditions in htaccess and if it was true use some rewrite rules. for example my idea is:

if(subdomain='en' OR query string have '[lan=en]'){
    RewriteRule ^home.html$ index.php [QSA,L]
    RewriteRule ^user/1.html$ user.php?id=1 [QSA,L]
    .....
}elseif(subdomain='fa' OR query string have '[lan=fa]'){
    RewriteRule ^home.html$ index_fa.php [QSA,L]
    RewriteRule ^karbar/1.html$ user.php?id=1 [QSA,L]
    .....
}

how I can do it? Is any way to use different .htaccess file for each conditions too?

NOTE: my domain address and my query string are dynamic

Upvotes: 1

Views: 3767

Answers (2)

lanzz
lanzz

Reputation: 43158

You can combine a set of RewriteCond directives with multiple RewriteRule directives, but in a roundabout way:

RewriteCond %{HTTP_HOST} !^en\. [AND]
RewriteCond %{QUERY_STRING} !(^|&)lan=en(&|$)
RewriteRule .* - [S=2]
RewriteRule ^home\.html$ index.php [L]
RewriteRule ^user/1\.html$ user.php?id=1 [QSA,L]

RewriteCond %{HTTP_HOST} !^fa\. [AND]
RewriteCond %{QUERY_STRING} !(^|&)lan=fa(&|$)
RewriteRule .* - [S=2]
RewriteRule ^home\.html$ index_fa.php [L]
RewriteRule ^karbar/1\.html$ user.php?id=1 [QSA,L]

The [S=2] flag in the RewriteRule directives means "skip next 2 rules"; thus the rules read like this: "If HTTP_HOST does not start with en. and QUERY_STRING does not contain lan=en, skip the next two rules"; thus the next two rules are considered only when the above conditions do not match.

Upvotes: 1

anubhava
anubhava

Reputation: 784908

Enable mod_rewrite and .htaccess through httpd.conf and then use rules like this in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^en\. [OR]
RewriteCond %{QUERY_STRING} (^|&)lan=en(&|$)
RewriteRule ^home\.html$ index.php [L]

RewriteCond %{HTTP_HOST} ^en\. [OR]
RewriteCond %{QUERY_STRING} (^|&)lan=en(&|$)
RewriteRule ^user/1\.html$ user.php?id=1 [QSA,L]

RewriteCond %{HTTP_HOST} ^fa\. [OR]
RewriteCond %{QUERY_STRING} (^|&)lan=fa(&|$)
RewriteRule ^home\.html$ index_fa.php [L]

RewriteCond %{HTTP_HOST} ^fa\. [OR]
RewriteCond %{QUERY_STRING} (^|&)lan=fa(&|$)
RewriteRule ^karbar/1\.html$ user.php?id=1 [QSA,L]

Upvotes: 3

Related Questions