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?

  • axidort@lemmy.world
    link
    fedilink
    arrow-up
    1
    ·
    2 days 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.

    • plaeiou@piefed.socialOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      7 days ago

      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

  • ultimaluz@lemmy.world
    link
    fedilink
    arrow-up
    2
    ·
    6 days ago

    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 do