Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

429
Views
How to have different .htaccess for different domains/request urls?

I work as a web developer, creating custom websites for customers. My .htaccess files are generally very simple. I generally only do 2 things:

  1. Create a custom ErrorDocument for 404 errors
  2. Redirect the /upload directory to /admin/upload to simplify some shared code between the public website and the admin system

I have everything working okay, other than one slight annoyance I'm wondering if anything can be done about. I'm using php, and I try to design the websites in a modular fashion, so that it doesn't matter whether the code is hosted at "example.com/" (the real domain), "localhost/exampleproject2" (on my development machine), or "mycompanywebsite.com/example.com" (Where we host a "beta" version for the customer to view when we get to that point in development).

From the PHP side of things, I have no issues here. But so far as Apache is concerned, I have to create 3 slightly different .htaccess files for each of those domains:

ErrorDocument 404 /404.php

Redirect 301 /upload /admin/upload
ErrorDocument 404 /exampleproject2/404.php

Redirect 301 /exampleproject2/upload /exampleproject2/admin/upload
ErrorDocument 404 /example.com/404.php

Redirect 301 /example.com/upload /example.com/admin/upload

Obviously this is not a huge deal, but it is a bit annoying. Mostly when I or someone else transfers files over FTP and accidentally overwrites the .htaccess from one environment with it's counterpart from another environment, and the site breaks. Also it means I can't include .htaccess in my git repository without possibly breaking things.

From my testing, relative urls don't seem to work at all. I think maybe Apache just doesn't support relative urls in .thaccess files for Redirects and ErrorDocuments. At least that's the way it seems. I get a server error when I try.

I'd like a way to have a single .htaccess file I can share between all 3 environments and include in the git repository. If anyone knows a good way to accomplish this that would be awesome. Solutions don't have to use .htaccess, I'm open to any solution that makes my code less dependent on absolute paths or urls.

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!