Hey all, rookie dev here!
I want to set up a website example.net which holds all my projects, but I’d also like to have a domain www.project.test which actually points to a single file on my example.net: example.net/project.html I’d also like for project.test/* to return an error at project.test/404 while example.net/* works normally.
basically project.test should be a url specifically for one (or a subset of) html files which live on example.net.
I figured that I can probably achieve this with the .htaccess file on the shared hosting, apache server my website is on. However the copy/pasteable rules I find online don’t exactly fit my needs. I have already set up both domains to point to my website, now I’m kind of stuck… (I only know html/css and some vanilla js at this point)
Could someone point me to useful (possibly beginner friendly) resources, where I could learn to create my own .htaccess rules / where the syntax is explained in detail? Do you have tips on how to approach this idea?
Any help appreciated!
edit: I’m also not familiar with regex yet , if I need an understanding of those aswell to achieve what i want?
yeah you can do it with just .htaccess, both domains land in the same folder so you check the Host header with mod_rewrite. something like this at the top of the .htaccess in your web root:
RewriteEngine On # project.test root shows project.html RewriteCond %{HTTP_HOST} ^(www\.)?project\.test$ [NC] RewriteRule ^$ /project.html [L] # anything else on project.test gives a 404 RewriteCond %{HTTP_HOST} ^(www\.)?project\.test$ [NC] RewriteCond %{REQUEST_URI} !^/project\.html$ RewriteRule ^ - [R=404,L]example.net doesn’t match those conditions so it keeps working as normal. and yes it’s a bit of regex but only that much, the backslash is just so the dot means a literal dot. one gotcha, if project.html loads css, js or images they’ll 404 too on project.test, so either inline them or add another
RewriteCond %{REQUEST_URI} !^/project-assets/line before the last rule and keep them in that folder. I do this kind of thing on shared apache hosting at work quite often, only thing to check is that your host has mod_rewrite on, almost all doI don’t really use apache, but to me it would be easier to use virtual hosts. You still leave the DNS A records as they are, just change what the server does when the Host header is a given value.
https://httpd.apache.org/docs/current/vhosts/name-based.html#using
TY , this works if I put the files project.example would point to in a subdirectory of the website (I don’t seem to have more granular control with my hosting provider). For this project I’d prefer to have no subdirectories though - If I want project.example to point at any single html file/ a group of html files which are located in the main folder of my site I think I’ll have to manage with .htaccess


