Php external link process - php

I'm building a site that that uses php and requires passing link id of each external link through the url and extracting the id using $_GET whenever a user clicks on any of the external links. Now, I need to rewrite the external url to momentarily point to my domain to enable me extract the link id before redirecting to the external destination, like what google does with search results before redirecting. Any help on how to do this will be highly appreciated.

I hope I understood what you were asking...
Have a database table of the conversions (or rather: the outside links) - the primary id can be the redirect "id".
Then have a file that you use for this outside-linking, eg "outsidelink.php".
Every link then needs to be written as outside.php?id=xxx and in the outsidelink.php you simply use the header() function to redirect the visitor (after you've recorded the fact of the request with all the details you want to; eg IP or userID, time, etc...)
Edit: oh, you first want to rewrite the external urls. I see. My bad.
Well, assuming the data where the external URL appears is some 'content' that comes from database, you need to scan the text before output for http://, compare the url (build a regex for this), if it looks like it's not matching the local host's url(s), then put it away in the aforementioned database, and rewrite the url to outsidelink.php?id=xxx
hope this time I got it right for you :)

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.

Passing URL (containing variables) as a string variable through URL

I know how to pass a simple URL as a string variable through a URL (i.e. http://example.com/?site=http://amazon.com), however I have a bit more of a complicated situation which I cannot figure out.... what if the url I want to be passing contains variables of its own? i.e. http://amazon.com/?ref=00112233&size=med ?
Background: I have a Wordpress site where I have a box to input an affiliate link, which is attached to a button that a user would click. Rather than just putting in the affiliate link so that they are sent directly to the site, I instead want to pass the affiliate link to a php script which would alter the affiliate link based on their geographic location, and then redirect them to the altered location. For example, my affiliate link is: http://amazon.com/?ref=00112233&size=med ... I want to pass that to my script, and then say if the user was from Canada it would alter the link to be http://amazon.ca/?ref=998877&size=med (switches domain and referral #) and send them to that location.
When I input the following URL to attach to my button, http://example.com/?site=http://amazon.com/?ref=00112233&size=med the redirect doesn't work..FF comes up with a msg saying the redirect is setup in a way that will never complete. The redirect works fine if the site value is something simple like 'http://amazon.com'... so I'm assuming it's because the affiliate link itself contains a ? already.
Any thoughts on how to get around this? I can't find anything that addresses this circumstance.
Hopefully that wasn't too confusing--let me know if any clarification is needed.
Thank you!

htaccess or php redirect to this task?

I need help from someone with experience :)
I have noticed that lot of sites with SEO friendly urls like www.site.com/123456/some-title only take the value 123456 and doesnt matter the rest, htaccess just send the variable 123456 to the specific url and then the visitor see the content.
So, if I type
www.site.com/123456/some-title-1
www.site.com/123456/some-title-2
www.site.com/123456/some-title-3
www.site.com/123456/some-title-4
I will always see the same page because the id passed is always the same 123456.
But I saw a few sites correcting this. For example:
-If I type any of the 4 urls above, they show me the content BUT they redirect me to the right url (www.site.com/123456/some-title) first.
How can I do that? I think this is a php question, since they need to connect to the database to check the original "url path" for the id 123456, right?
If someone can give me a tip to do that would be great!
Thanks
How can I do that? I think this is a php question, since they need to connect to the database to check the original "url path" for the id 123456, right?
This is a PHP question. 2 things need to happen here. When someone requests:
www.site.com/123456/some-title-1
There is routing that happens in htaccess, something like (example rules):
RewriteRule ^([0-9]+)/(.*)$ /some.php?id=$1&slug=$2 [L]
So whatever the script is (some.php), it'll get two parameers, an ID and a slug. The ID is what it uses to do a lookup into a database or wherever the actual content is stored. When it fetches that information from the database, it can compare slugs. If they differ, then instead of serving the content, redirect the browser to show the correct slug. Then we start back over from the beginning, browser submits request (this time with the right slug for the ID), rewrite rule routes it to some.php and because the slug is correct, the actual content gets served to the browser.

How to make form that creates a new URL in a website?

Example, on Craigslist users complete a form of what theyre looking for and once submitted it automatically creates a new URL hosted on the website URL. Sorry i don't know how to do this or what it is called. Anyone? How can i do this and what is it called? =) Thanks!
You mean like how StackOverflow has a URL for this question?
http://stackoverflow.com/questions/12662640/how-to-make-form-that-creates-a-new-url-in-a-website
All it takes is a simple .htaccess file, something like:
RewriteEngine on
RewriteRule /questions/([0-9]+).*$ /question.php?id=$1
The whole "how-to-make..." thing is just the name of the question. I'm not entirely sure why many sites put that in the URL, possibly for SEO purposes, but it will automatically be filled in if you leave it out so I'm assuming it's optional.
This is done by a (mysql-)database, php and .htaccess resulting in dynamic url's.
A dynamic URL is a page address that results from the search of a database-driven web site or the URL of a web site that runs a script. In contrast to static URLs, in which the contents of the web page stay the same unless the changes are hard-coded into the HTML, dynamic URLs are generated from specific queries to a site's database. The dynamic page is basically only a template in which to display the results of the database query. Instead of changing information in the HTML code, the data is changed in the database.

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

Categories