0

I have a specific requirement and am looking for suggestions on the best possible way to achieve that. I would start by apologizing if I sound too naïve. What I am trying to achieve in here is:

A) I have a parent site, say, www.abc.com. B) I am planning to enable multisite option for it. This parent site has a area map with a number of location images overlayed. All of these images, when clicked, should lead to a subsite.

C) This subsite (has already been coded) is totally dynamic and every single information being displayed on it is being extracted from the database. It uses a session variable, which for now has been hard-coded at the very beginning of the header. This variable also decides on which database to refer to. So it will display information for different locations, based on the location selected on the parent site. Even the URL should appear per that. Say if Location ‘A’ was clicked on parent-site then the session variable needs to set to ‘LocA’ on the sub-site and the URL should be something like www.abc.com/LocA and if the Location ‘B’ was clicked then the session variable should be set to ‘LocB’ and the URL should appear as www.abc.com/LocB etc.. Trying to figure out how to achieve this. [It will have one front-end for all the locations but different databases for each location.]

I am an entrepreneur with some programming experience from my past (but none related to website designing). Because of the help from all you geniuses and the code samples lying around, I was able to code the parent site and the sub-site (using html, php, js, css ). Now the trouble is how to put it all together and make it work in correlation. Though it will still be a week or two before I get to try it but I am trying to gather insights so that I am ready by the time I reach there. Any help will be deeply appreciated.

  • 1
    As far as a generic web server is concerned, there isn't really such a thing as a "sub-site", or even a "website" at all. The web server receives a request for a particular URL, and returns a response - URLs aren't just something that "appears" in the browser, they are the most fundamental piece of every request. Generally, it looks at the hostname ("www.abc.com" in your example) first, then looks for how you've configured that hostname. That might be "each unique URL maps to a unique file on disk", or "all requests map to the same PHP script", or anywhere in between. – IMSoP Aug 25 '22 at 18:37
  • 1
    You might find [this reference page about rewrite rules](https://stackoverflow.com/q/20563772/157957) useful; but there's really not enough information here to know _what_ you need. – IMSoP Aug 25 '22 at 18:38
  • The information is helpful and has given me food for thought. I understand now that I can re-route the URL's by making changes to htaccess. Though, it may take a few attempts on my side to get the desired output. This may have answered the first half of my query. Please help me with the second part here. I will try to rephrase it. When a user clicks on an image on the parent site, www.abc.com, say, it calls for loading www.abc.com/LocA which would get rerouted to www.abc.com/GenericLoc [this would be a child-site installed in a subdirectory]. contd. – Jack Gedreven Aug 26 '22 at 04:51
  • How can I pass the value 'LocA' to the child-site '/GenericLoc' while re-routing, for it to display information in accordance? Or is it even possible? [Forgive my naiveness here. TIA] – Jack Gedreven Aug 26 '22 at 04:55
  • [Trying to further elaborate] There are multiple images on parent-site. Clicking one would call for /LocA, second may try to invoke /LocB, Third, LocC and so on.. all will get re-routed to /GenericLoc. The variables passed to it ('LocA', 'LocB', 'LocC... etc) will tell it what to do. – Jack Gedreven Aug 26 '22 at 05:15
  • 1
    What do you mean by "rerouted"? Do you mean that after the user has visited one of those specific URLs, they end up at /GenericLoc, but different content displays? You already mention the answer to that - a session variable; or just a plain cookie, if you prefer. However, maybe I could persuade you that it sounds like a horrible user experience - it means that bookmarks are useless, incoming links are useless, browser history breaks if you go between subsites, etc. Search engines will also struggle to index the pages properly, which some people seem to care about more than human happiness. – IMSoP Aug 26 '22 at 06:39
  • Do you mean that after the user has visited one of those specific URLs, they end up at /GenericLoc, but different content displays? - Precisely. That is the dilemma.. all the locations will have same structure but different data to display.. hence different db. If I create multiple subdirectories for each location, then everytime I would add or edit a feature, I will need to do that to all the subdirectories of all locations. As for session variable, I have session enabled only in the subdirectory and not in the parent-site.. so not sure if I can use it.. – Jack Gedreven Aug 26 '22 at 08:39
  • would you suggest that i enable it in the parent-site itself? Also, cookies sound like a good option but is it a reliable option and would work without fail each time?... asking this due to my lack of experience with them... – Jack Gedreven Aug 26 '22 at 08:42

1 Answers1

1

I think the fundamental thing to understand before you get deeper is what a URL is. A URL is not part of the content that you display to the user; nor is it the name of a file on your server. A URL is the identifier the user sends your server, which your server can use to decide what content to serve. The existence of "sub-sites", and "databases", and even "files" is completely invisible to the end user, and you can arrange them however you like; you just need to tell the server how to respond to different URLs.

While it is possible to have the same URL serve different content to different users, based on cookies or other means of identifying a user, having entire sites "hidden" behind such conditions is generally a bad idea: it means users can't bookmark that content, or share it with others; and it probably means it won't show up in search results, which need a URL to link to.

When you don't want to map URLs directly to files and folders, the common approach involves two things:

  1. Rewrite rules, which essentially say "when the user requests URL x, pretend they requested URL y instead".
  2. Server-side code that acts as a "front controller", looking at the (rewritten) URL that was requested, and deciding what content to serve.

As a simple example:

  1. The user requests /abc/holidays/spain
  2. An Apache server is configured with RewriteRule /(...)/holidays/(.*) /show-holidays.php?site=$1&destination=$2 so expands it to /show-holidays.php?site=abc&destination=spain
  3. The show-holidays.php script looks at the parameter $_GET['site'] and loads the configuration for sub-site "abc"
  4. It then looks at $_GET['destination'] and loads the appropriate content
  5. The output of the PHP script is sent back to the user
  6. If the user requests /def/holidays/portugal, they will get different content, but the same PHP script will generate it

Both the rewrite rules and the server-side script can be as simple or as complex as you like - some sites have a single PHP script which accepts all responses, looks at the real URL that was requested, and decides what to do; others have a long list of mappings from URLs to specific PHP scripts.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • I think I am getting a gist of it. Will weigh the pros and cons before proceeding further. Still, just to clarify.. if I decide to reroute and plan on using the front controller, then I will still need to mention the Rewrite rules.. correct? Where will the front-controller PHP go... in parent site directory or the sub-directory? lastly, is there some reference material that you may be able to point me to that can help me do more research on this front-controller option? Thanks!! – Jack Gedreven Aug 26 '22 at 10:56
  • **.htaccess** 'RewriteEngine On RewriteRule . /front-controller.php' **front-controller.php** 'Conditions' *does that sound right?* – Jack Gedreven Aug 26 '22 at 11:00
  • 1
    @JackGedreven Yes, you will need at least one rewrite rule, to say "all requests should be passed to this PHP script"; then logic in that PHP script to decide what to do next. I don't know of any good references for the concept; search for "PHP front controller" and "PHP URL routing" will probably find plenty. [Nikita Popov's FastRoute package](https://github.com/nikic/FastRoute) is a popular option for the "define all the rules in PHP" approach. – IMSoP Aug 26 '22 at 11:37
  • Thanks a ton, mate... you are a saviour :) – Jack Gedreven Aug 26 '22 at 13:06