.htaccess rewrite subdomain to dynamic url

I'm using htaccess mod-rewrite to rewrite a subdomain url to a dynamic url, for example: subdomain.example.com/shop is rewritten to read example.com/shop/?page=subdomain

The following code is working fine:

RewriteEngine On
RewriteCond %{HTTP_HOST} (.*)\.example\.com [NC]
RewriteRule ^(.*)$ http://example\.com/$1/?page=%1 [L,P]

But the problem is if we access: subdomain.example.com (without the "shop" at end of the url), it display a 404 error page because it reads the url as: example.com//page=subdomain with double slashes "//" because the "$1" is empty.

Is there anyway that we can put "$1" in a condition that if it's empty then slash "/" next to it will be removed as well so that the url should be read as example.com/page=subdomain with only one slash before the "page" parameter?

Really appreciate if any htaccess experts could help me out.

Thank you!


Sure, just add a separate rule explicitly for the "nothing there" case:

RewriteCond %{HTTP_HOST} (.*)\.example\.com [NC]
RewriteRule ^$ http://example\.com/?whatever [L,P]

Need Your Help

Send values get from append forms to spring controller

spring model-view-controller

I was creating a model where the structure as follows: