Kelvin
Kelvin

Reputation: 585

.htaccess Rewrite for different domains

Below is my tried existing .htaccess rules setting

RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^api/([a-z]{1,3})/([0-9a-zA-Z]{1,15})$ api.php?ac=$2&type=$1 [L]
RewriteRule ^s/([0-9a-zA-Z]{1,15})$ init.php?ac=$1&type=s [L]
RewriteCond %{REQUEST_URI} !^.*\.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ %{REQUEST_FILENAME}.php

I park several domains exampleA.com and exampleB.com to example.com

I want to rewrite the below cases

example.com/s -> error 404 page
example.com/s/ -> error 404 page
example.com/s/abc -> init.php?type=s&account=abc
example.com/api/s/abc -> api.php?type=s&account=abc

Other than example.com

exampleA.com -> init.php?type=d
exampleB.com -> init.php?type=d
exampleA.com/abc -> init.php?type=d&account=abc
exampleB.com/abc -> init.php?type=d&account=abc
exampleA.com/api/abc -> api.php?type=d&account=abc
exampleB.com/api/abc -> api.php?type=d&account=abc

How do I write the RewriteCond and RewriteRule to archive this?

Upvotes: 1

Views: 96

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133770

With your shown attempts, please try following htaccess rules in your rules file. Make sure you have cleared your browser cache before testing your URLs. This also considers that your all multiple subdomains have same folder path.

RewriteEngine ON
##Apply https to urls.
RewriteCond %{HTTPS} off 
RewriteRule ^(.*)/?$ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]

##Rules for example.com/s OR example.com/s/ to redirect to 404 here.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^s/?$ - [R=404,NC,L]

##Rules for example.com/s/abc -> init.php?type=s&account=abc
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(s)/(abc)/?$ init.php?type=$1&account=$2 [NC,QSA,L]

##Rules for example.com/api/s/abc -> api.php?type=s&account=abc
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(api)/(s)/(abc)/?$ $1.php?type=$2&account=$3 [NC,QSA,L]

##Rules for exampleA.com -> init.php?type=d AND for exampleB.com -> init.php?type=d
RewriteCond %{HTTP_HOST} ^example(A|B)\.com$ [NC]
RewriteRule ^/?$ init.php?type=d [L]

##Rules for exampleA.com/abc -> init.php?type=d&account=abc AND for exampleB.com/abc -> init.php?type=d&account=abc
RewriteCond %{HTTP_HOST} ^example(A|B)\.com$ [NC]
RewriteRule ^abc/?$ init.php?type=d&account=abc

##Rules for exampleA.com/api/abc -> api.php?type=d&account=abc AND for exampleB.com/api/abc -> api.php?type=d&account=abc
RewriteCond %{HTTP_HOST} ^example(A|B)\.com$ [NC]
RewriteRule ^(api)/(abc)/?$ $1.php?type=d&account=$2 [NC,QSA,L]

Upvotes: 1

Related Questions