0

I'm currently using sub-domains to segment clients to their own websites - this is only for appearance to show the client name in the URL. However, there is no need to use Subdomains. Instead, I'd like to have 1 website and rewrite the url so they each client's name appears like this in the URL like this: https://test.com/CLIENT_NAME/index.php

When the client first visits https://test.com/CLIENT_NAME/ if the session variable "CLIENT_NAME" !isset, I'd like to route them to test.com/index.php?id=CLIENT_NAME (but keep the URL showing as https://test.com/CLIENT_NAME/) so I can use the $_GET variable from "id" to get their data from the database and set the session variables.

Once logged in and the session variable of CLIENT_NAME is set, I'd just like to rewrite each URL to have /CLIENT_NAME/ after the base domain, but effectly the site will all pages as though it's just the base domain (without /CLIENT_NAME/).

For example once logged in: the URL will appear as test.com/CLIENT_NAME/page_1 but the server will access the file stored in the root public_html folder that would usually be accessed by visiting test.com/page_1.php.

At the same time, it would be great to remove ".php" from the end of each webpage (like in the above example).

Thanks for your assistance!

I've tried all sorts of other examples found on here, but it seems everyone uses a fixed parameter for the RewriteBase, instead of checking the session variables to see if something is set there and then using that value as the RewriteBase if it is, or redirecting the user to a GET request (with the url masked) to set the session variables.

  • Have you tried to implement this? You wouldn't need to use `RewriteBase` here. You also can't rewrite in Apache/`.htaccess` based on a PHP session variable - Apache has no access to the "PHP session". You could, however, rewrite based on a cookie instead - would that work for you? – MrWhite Jan 13 '23 at 13:35
  • Personally I'd point you to a single router script which means you can perform _all_ of your logic in PHP. See [this](https://stackoverflow.com/a/70871520/231316), specifically the part about `$_SERVER['REQUEST_URI']`. – Chris Haas Jan 13 '23 at 15:10
  • Thank you so much for your comments. I have spent a lot of time checking out the post you recommended @ChrisHaas. I am struggling to understand it though. I think in the case of the other question, they were looking to route based on a specific subfolder. I can't work out how to write the rewrite rule to take "test.com/any_company_name_here/index.php" and direct it to a specific PHP file where I can write PHP code using $_SERVER['REQUEST_URI'] to set the session variables. Any ideas? Thanks again!! – Gregsta Jan 14 '23 at 12:13
  • The htaccess for rules for a router are in [this answer](https://stackoverflow.com/a/59735660/231316). It basically says "if you can't match the request exactly to something on disk, such as an image, then send the request to index.php". In that file you parse `$_SERVER['REQUEST_URI']` and do your code stuff. You no longer send people to specific PHP files, just "routes". – Chris Haas Jan 14 '23 at 14:50

0 Answers0