same php file to serve 3 domains - php

I'm rather new to php and wonder if the following technically feasible?
I would like to have 3 urls to hit the same page on the server, but display different slightly content (content text is in stored in database tables with a indicator flag whether the content belongs to urlA or urlB), i.e.
URL_A will show content specific to A's products only, URL_B will show content specific to B's products only, URL_C will show products for both A and B. All these URLs will point to the same page (index.php) and I would prefer if the domains all remain as they are entered to differentiate the branding and content, rather than using 301 to redirect 2 of the domains (e.g. a visitor entering www.urlA.com will be able to continue browsing/navigating the website pages like www.urlA.com/about-us or www.urlA.com/news)
Can this be implemented using a mod-rewrite and some php logic or passing some session or hidden form input to various pages? Any advice on where/how to start would be appreciated :)

You can use $_SERVER['HOST_NAME'] to determine which virtual host is being accessed. Use this value to perform whatever logic you require.

Related

how to redirect all subsite urls to one single url in a multi-site and also send a variable/value to this sub-site

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.
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:
Rewrite rules, which essentially say "when the user requests URL x, pretend they requested URL y instead".
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:
The user requests /abc/holidays/spain
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
The show-holidays.php script looks at the parameter $_GET['site'] and loads the configuration for sub-site "abc"
It then looks at $_GET['destination'] and loads the appropriate content
The output of the PHP script is sent back to the user
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.

Masking $_GET variables of the URL when passing pages in PHP

I'm trying to make the URLs of my site SEO and user friendly. It is basically a static corporate website but for the side menu I am passing some variables through URL to show the sub menu of main selected menu.
For example: Offshore staffing is one of the main menu items and one of its sub menu items is Programmers. When someone clicks Programmers I will pass the id of main menu and sub menu through URL to collapse all other menus and promote the opened menu.
I want to mask something like ?id=4&sid=4 at the end of every URL. Can't use hidden input element because I am modifying this site and the developer who actually built that site didn't use forms.
You're looking for using a .htaccess file to rewrite URL's. For example stackoverflow might use something like this:
RewriteEngine on
RewriteRule ^questions/([0-9]+)/([_\-\&\'\,\+A-Za-z0-9-]+)?$ questions.php?q=$1
This would make both stackoverflow.com/questsions/1234/a-title-of-a-page and stackoverflow.com/questions.php?q=1234 the same page, so on your website you would need to use the "tidy" version of the URL (the first one)
A lot more can be read into this and you can customize you're URL's to what you require. For example, a few places to read up on it include:
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
http://www.easymodrewrite.com/
Generally a good way to do this (so that you don't have lots of ID's in your URL's) is to store a "URL friendly" name of the page (e.g. "name-of-page") in your database, then when the page is requested, just search your database for that name and you'll know what ID it relates to.
Translate the ID's to the words they stand for when writing the links, and use mod_rewrite to pass them back to PHP when they're visited, where you do a lookup based on the words, and find the ID again.
So the link becomes /offshore/programmers, then you do a lookup for the ID's of "offshore" and "programmers" and show the appropriate content.
What do you mean by "masking"? Is URL-rewriting, what you are searching for? If yes, you need to append the alias (e.g. "programmers") to the URL, instead of the GET-params. Those will be translated to GET-params via URL-rewriting and then matched to an ID via PHP.

Displaying different content on a Profile page when the logged-in user is the Profile owner

I always try to make code nicer and more maintainable but I'm not sure if I do it right... For example, I'm doing a social network site, so the page domain.com/profile?id=17 would open profile for user id 17. If the user is the currently logged in one, then the profile will be seen as the owner (with edit options, etc) otherwise the page would show a profile for outsiders (no edit options).
How do I do this nicely? I was thinking that after checking if the user id == the user id in $_SESSION, I include one php file (the one with the edit options), otherwise I include the other one. Is this a solution? Is there a better one?
I do the same thing for headers, I have a file 'header.php' that includes the header of all pages (some php but also a logo, etc), however I'm not sure if this is good since a user can go to his browser and type mydomain.com/header.php and then be displayed a header only.
Your solution is one way to do it .another way to do it would e just check the user_id in session and if it is not set then just display another html . Also you would want to access users as their usernames rather than user id.For example instead of user_id.php=17 to username/luqita.If you are using apache as the web server then you could use it's mod_rewite module to generate pretty urls.

How to redirect a Google search result to a dynamic Web page?

I'm trying to enter a list of items into Google Base via an XML feed so that, when a user searches for one of these items and then clicks the search result link in Google Base (or plain Google), the user is directed to a dynamic Web page on my Web site. I'm assuming that the only way to specify a specific link (either static or dynamic) is through the attribute in the XML feed. Is that correct? So, for example, if my attribute is:
http://www.example.com/product1-info.html
the user will be directed to the product1-info.html page.
But if, instead of a static product page, I want to have the user redirected to a dynamic page that generates search results from my local database (on my Web site) for all products containing the keyword "product1", would I be able to do something like this?:
http://www.example.com/products.php?productID=product1
Finally, and most importantly, is there any way to specify this landing page (or any specific landing page) from a "regular" Google search? Or is it only possible via Google Base and the attribute? In other words, if I put a bunch of stuff into Google Base, if any of it shows up in a regular Google search, is there a way for me to control what parameters get passed to the landing page (and thus, what search is performed on the landing page), or is that out of my control? I hope I explained this correctly. Thanks in advance for any help.
first question: Yes, urls containing a query_string part are allowed.
http://base.google.com/support/bin/answer.py?hl=en&answer=78170 says:XML example:
<link>http://www.example.com/asp/sp.asp?cat=12&id=1030</link>
--
Let me rephrase the second question to see if I understand it correctly (might be completely on the wrong track): E.g. products.php?productID=product1 performs a db-search for the product "FooEx" and products.php?productID=product2 for "BarPlus". Now you want google to show the link .../products.php?productID=product1 but not ....?productId=product2 if someone searched for "FooEx" and google decided that your site is relevant? Then it's the same "problem" we all face with search engines: communicate what each url is relevant for. I.e. e.g. have the appropriate (and only the appropriate) keywords appear in the title/h1 element of the page, avoid linking to the same contents with different urls (e.g. product.php?x=1&productId=1 <-> product.php?productId=1&x1, different urls requesting most probably the exact same contents), submit a sitemap, and so on and on....
edit:
and you can avoid the query-string part all together by using something like mod_rewrite (e.g. the front controller for the zend framework makes use of it) or by parsing the contents of $_SERVER["PATH_INFO"] (this requires the webserver to provide that information), e.g. http://localhoast/test.php/foo/bar -> $_SERVER['PATH_INFO']=='/foo/bar'
Also take a look at the link to this thread: How to redirect a Google search result to a dynamic Web page?, it contains the title of the thread, but SO is perfectly happy with How to redirect a Google search result to a dynamic Web page?, too. The title is "only" additional data for search engines and (even more) the user.
You can do the same:
http://www.example.com/products.php/product1/FooEx <-> http://www.example.com/products.php/product2/BarPlus

How to Have Search Engines Index Database-Driven Content?

How can I make it so that content from a database is available to search engines, like google, for indexing?
Example:
Table in mysql has a field named 'Headline' which equals 'BMW M3 2005'.
My site name is 'MySite'
User enters 'BMW M3 2005 MySite' in google and the record will show up with results?
Google indexes web pages, so you will need to have a page for each of your records, this doesn't mean to say you need to create 1,000 HTML pages, but following my advice above will dynamically / easily provide a seemingly unique page for each product.
For example:
www.mydomain.com/buy/123/nice-bmw-m3-2005
You can use .htaccess to change this link to:
www.mydomain.com/product.php?id=123
Within this script you can dynamically create each page with up-to-date information by querying your database based on the product id in this case 123.
Your script will provide each record with it's own title ('Nice BMW M3 2005'), a nice friendly URL ('www.mydomain.com/buy/123/nice-bmw-m3-2006') and you can include the correct meta information too, as well as images, reviews etc etc.
Job done, and you don't have to create hundreds of static HTML pages.
For more information on .htaccess, check out this tutorial.
What ILMV is trying to explain is that you have to have HTML pages that Google and other search engines can 'crawl' in order for them to index your content.
Since your information is loading dynamically from a database, you will need to use a server-side language like PHP to dynamically load information from the database and then output that information to an HTML page.
You have any number of options for how to accomplish this specifically, ILMV's suggestion is one of the better ways though.
Basically what you need to do first is figure out how to pull the information from the database and then use PHP (or another server-side language) to output the information to an HTML page.
Then you will need to determine whether you want to use the uglier, default url style for php driven pages:
mysite.com/products.php?id=123
But this url is not very user or search engine friendly and will result in your content not being indexed very well.
Or you can use some sort of URL rewriting mechanism (mod_rewrite in a .htaccess file does this or you can look at more complex PHP oriented solutions like Zend Framework that provide what's called a Front Controller to handle mapping of all requests) to make it so that your url's look like:
mysite.com/products/123/nice-bmw-m3-2006
This is what ILMV is talking about with regard to url masking.
Using this method of dynamically loading content will allow you to develop a single page to load the information for a number of different products based on the Id thus making it seem to the various search engines as though you have a unique page for each product.

Categories