Parth Soni
Parth Soni

Reputation: 11648

.htaccess redirect path & subdomain as query parameter

I want to redirect a path to a file as query parameter.

For ex:

http://subdomain.example.com/helloworld/

Should redirected to

/index.php?domainParam=subdomain&param1=helloworld

I have tried multiple ways but they either helps with subdomain or with the path. But I want both of them to be redirected as a parameter in a query string.

Like:

RewriteCond %{HTTP_HOST} !^app\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteRule ^$ index.php?domainParam=%1 [L]

RewriteCond %{HTTP_HOST} !^app\.example\.com$ [NC]
RewriteRule ^([^/]+)/? index.php?source=$1 [L]

But it is not helping when redirecting both of them as parameters.

Need your assistance to merge them both.

Upvotes: 1

Views: 53

Answers (1)

anubhava
anubhava

Reputation: 785128

You may use this rule for this task:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(?!app\.)([^.]+)\.example\.com$ [NC]
RewriteRule .* index.php?domainParam=%1&param1=$0 [L,QSA]

Upvotes: 1

Related Questions