If all your subdomains are pointing to same code source, then it is better to handle it with .htaccess
(I was not sure if this question should be posted to Developing with WordPress forum, hence posting it here)
It’s not Developing or Everything else WordPress. It’s Fixing WordPress and that is where I moved your topic.
Thanks @moderator
@ddsuresh , I’ve put following rewrite rules in .htaccess. But it’s not working as per my requirement.
RewriteCond %{HTTP_HOST} ^(.+\.)?demo\.myprods\.com
RewriteRule . - [L]
RewriteCond %{HTTP_HOST} !^myprods\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.myprods\.com [NC]
RewriteRule . https://myprods.com/category/%1 [R,L]
The idea here is that, the first rule will ignore anything that points to demo site and let it get processed by the separate site hosted at demo.myprods.com.
The second rule is supposed to capture the subsite name and pass it to the redirect URL. But when I access https://type1.myprods.com, the URL doesn’t change and I get the “site can’t be reached” error.
I’m not able to figure out what’s wrong here. Any help?
@ddsuresh my bad … I had forgotten to add DNS entry for subdomains 🙁 … after adding CNAME entry for *.myprods.com to point to myprods.com “site can’t be reached” error is gone.
But now when I access https://type1.myprods.com, instead of showing my website, its showing me my webhost’s custom home page. I tried commenting my changes in .htaccess and still getting same behavior (webhost’s custom home page). which makes me think that the rewrite rule is not getting applied / .htaccess is not getting into picture at all.
Is that correct understanding?
Anyway I’ve raised a support ticket at my webhost. Hopefully will get a solution soon.
For those who come across similar issue …
My webhost admin asked me to create wildcard subdomain using cPanel.
So I first removed the CNAME entry I had added manually. Then created wildcard subdomain from cPanel. Just to make it easier to handle, I created separate document root to handle all subdomain requests. In this folder I added .htaccess that is supposed to redirect user to main site.
Also added index.php to understand if my .htaccess is getting applied or not.
Initially this index.php was getting displayed. After some more testing I realized that the rewrite rule should have been like:
RewriteRule .? https://myprods.com/category/%1 [R,L]
The ? was missing and hence it was not getting applied, effectively showing my index.php.
After updating this rewrite rule, all subdomains / URLs like https://type1.myprods.com are correctly getting redirected to https://myprods.com/category/type1, while keeping the demo site working separately.