Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dgo's avatar
Level 6

sub.mydomain.com redirect to mydomain.com/sub with Forge|Digital Ocean

I have wildcard subdomains setup with ssl cert. I want to accomplish this:

sub.mydomain.com redirects 301 to mydomain.com/sub

Seems like this should be easy... but

1. Can't add /sub to a dns record at Digital Ocean.

2. Can't get the Forge redirects from the panel to work (probably syntax).

3. Don't know enough to edit the Nginx Config file

***bonus - would love to have subs not redirected to hit a 404 location...

0 likes
2 replies
joshhanley's avatar

I would have a look into sub domain routing in the docs.

You can't add "/sub" to DNS records. Instead you would need to setup a wildcard (*.mydomain.com) CNAME record to point to your main app domain and in Forge check "Allow Wildcard Sub-Domains" when setting up your site.

Then you can use subdomain-routing as per link above and check for the existence of a sub domain and redirect and 404 if it doesn't exist.

RamjithAp's avatar

Try this in your .htacess file

RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$ 
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteRule (.+)$ "http://example.com/%1" [R=301,L]

Please or to participate in this conversation.