axidort

joined 1 day ago
[โ€“] axidort@lemmy.world 1 points 18 hours ago

Since you can't use vhosts or subdirectories on that host, .htaccess can do it with mod_rewrite. Something like this in the .htaccess in your main folder (adjust the domain and filename):

RewriteEngine On

# project.test root -> serve the one file
RewriteCond %{HTTP_HOST} ^(www\.)?project\.test$ [NC]
RewriteRule ^$ /project.html [L]

# anything else on project.test -> 404
RewriteCond %{HTTP_HOST} ^(www\.)?project\.test$ [NC]
RewriteRule !^project\.html$ - [R=404,L]

How it works: each RewriteCond applies only to the RewriteRule right below it, and it checks the Host header, so example.net is never touched. The first rule serves project.html (internally, the URL in the browser stays project.test/) when someone visits the root. The second returns a 404 for every other path on that domain. The !^project\.html$ exception matters: Apache re-runs the rules after the internal rewrite, and without it the second rule would 404 your own page.

Two things to watch. If project.html uses CSS, JS or images, those requests also hit project.test and will 404, so either inline them or add them to the exception, e.g. !^(project\.html|style\.css)$. And in .htaccess the pattern has no leading slash (^$, not ^/$).

On regex: you don't need to learn it first. Here you only need ^ (start), $ (end), \. (a literal dot) and ! (not). Learn the rest as you go. For learning, the mod_rewrite intro and the RewriteCond/RewriteRule reference in the Apache docs are decent, and regex101.com lets you test patterns live. If something doesn't work, check the host's error log, and note that some shared hosts cache .htaccess changes for a minute or so.

[โ€“] axidort@lemmy.world 6 points 18 hours ago

A few things that haven't come up yet, since a lot depends on whether you want it to be just for you or something that can get real HTTPS.

  • For dev machines, use .test or .localhost. Both are reserved by RFC 6761, so they can never be registered by someone else and will never collide with a real site. .localhost also resolves to loopback by itself in most browsers, so myapp.localhost needs no hosts-file entry.
  • Avoid .local. As mentioned above, it's reserved for multicast DNS (RFC 6762), so on a lot of systems the lookup goes to mDNS instead of your DNS server and you get random slow or failing resolution. .lan and .home are popular but not reserved by any RFC, so a clash with real DNS is possible in principle.
  • For a home network, the standard answer is home.arpa (RFC 8375). It's reserved specifically for this, so nas.home.arpa is safe.
  • If you want valid certificates on LAN hosts (phones and some apps refuse custom root CAs), buy a cheap domain and use subdomains of it for internal hosts. You don't have to expose anything: use a DNS-01 challenge with Let's Encrypt, which proves ownership through a TXT record instead of an inbound HTTP request, and it also supports wildcard certs. Point the names at private IPs via your local DNS.
  • For purely local HTTPS on a dev machine, mkcert creates a local CA and certs for .test/.localhost names, trusted by your own browser, with no public domain required.

So: .localhost/.test plus mkcert on the dev box, and an owned domain with DNS-01 for anything you want proper certs on.