Dynamic links linking to root domain - php

I am building a site directory and I am having some trouble linking up to sites..
The directory currently stores the site domain in a table and calls it through a foreach loop listing 25 separate domains on the page, but when I click on the links I am greeted with
localhost/directory (my site root) /linkeddomain.com
Rather than just displaying linkeddomain.com
I put http://www. in front of the array call
href='http://www.".$row['siteurl']."
However this is useless for production because if anyone enters into their domain http://www.theirdomain.com it will come out as http://www.http://www.theirdomain.com
Does anyone know how to fix this issue?
Thanks in advance
Luke

Make sure that the base URL is always the full URL, including the scheme and a subdomain (if applicable).
So:
$base_url = "http://livesite.com";
$base_url = "http://localhost/john/customerX";
$base_url = "https://secure.livesite.com";
If all your links are prefix by the base URL you should be fine.
Note that in all URLs I left off the trailing /. You can chose to include it, just make sure you always do it in the same way - have a clear normalized form.

You can just check to see if you need to add http to the url or not.
if(!preg_match("^https?://", $url)){
$url .= "http://" . $url;
}
That will only add it if it is needed.

Related

PHP MySQL - URL Structure

I have a semi-professional website about my travels where I present all the pictures for each country.
For that I have a MySQL-Database containing the following attributes:
- country
- country-id
- pictures (links)
I am using a PHP-file which requests the pictures: "country.php"
So I get the URL-Structure: www.url.com/country/spain/ or www.url.com/country/usa/.
What I need is to change the URL Structure to www.url.com/spain but still maintain that "central" country.php-file.
I know a method which only works, when I have a folder for each Country in my webspace and an index.php-file which simply loads the country.php-file (I did that on another website). But in this case I would have way too many folders, which makes it all too complex since there are not only countries but also "special places and cities".
Is there another way to do that?
EDIT:
I got it with this code:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+)$ country.php?url=$1
But now the problem is, that whatever I type in, eg. www.url.com/xyz, the server thinks that I want to open the country "xyz". What can I do, to only rewrite, when the entered country is in my database?.
I am sorry if I ask too complicated, but I just didn't know how explain my problem better.
If you dont have any framework in use, which handles your routing, you could simply use the .htaccess file and set a rewrite rule.
Look here for example: how to remove folder name from url using htaccess
$url ="******YOUR URL HERE***********";
$urlArr = parse_url($url);
$urlbase = $urlArr['host'] . #$urlArr['path'];

drupal prepending 'node' to the link url

I am working on a drupal site. The current issue is that when a link is created either by using the url() or the base_path variable, it works ok on local development environment but when the very same code is put on the server, the url prepends node string in the path.
e.g. the path is <drupal site>/latestnews . The generated path is <drupal site>/node/latestnode and when clicked, it shows the page not found error. However would like to mention that the links work fine (i.e generate correct path) when clicked from the home page.
any help would be appreciated. If it helps, I am using Pantheon hosting for testing.
Does the server by chance have the pathologic module enabled? That module's job is to rewrite links for different locations. It may be misconfigured. If it is enabled, try disabling it, or adjusting its configuration in each of your text formats (admin/config/content/formats).
Have you checked if the $base_url variable in the 'sites/default/settings.php' file has the correct value? According to documentation "if Drupal is generating incorrect URLs on your site, which could
* be in HTML headers (links to CSS and JS files) or visible links on pages
* (such as in menus), uncomment the Base URL statement (remove the
* leading hash sign) and fill in the absolute URL to your Drupal installation."
I had the exact same problem and fixed it.
Initially, my site was NOT using clean urls. When I ENABLED clean urls and rewrote my links so that they weren't preceded by the string "?q=", this broke my hard coded links. This is because when clean urls are NOT used, pages appear to be in the root directory, but when clean urls ARE used, they appear to be inside of folders.
The following example shows how updating a page to use clean urls can affect it.
Clean_urls_DISABLED
Page: SITE?q=node/7
Hard_coded_link_in_page: ?q=node/9
Link_displayed_in_browser: SITE?q=node/9
Clean_urls_ENABLED
Page: SITE/node/7
Hard_coded_link_in_page: node/9
Link_displayed_in_browser: SITE/node/node/9
Notice that the page that was updated to use clean urls, has "node/node" in its link when it is displayed in the browser.
The solution is to prepend "../" to all hard coded urls (for pages other than your front page). That way hard coded links for other contents, such as images, will work as well.
Clean_urls_ENABLED
Page: SITE/node/7
Hard_coded_link_in_page: ../node/9
Link_displayed_in_browser: SITE/node/../node/9
Hard_coded_image_source_in_page: ../sites/default/files/img.jpg
Image_source_in_browser: SITE/node/../sites/default/files/img.jpg
This will work for hard coded paths for links, images, and any other elements that contain paths referencing files/documents.

PHP redirect to another website

I have a directory named "goto" and a file inside called index.php. Currently the following is inside the index.php file:
<?php
$url = $_GET['url'];
header("Location: $url");
?>
At the moment to redirect to another URL I have to type this into the address bar:
http://mysite.com/goto/?url=http://google.com
I would appreciate it if you could tell me how I could change that URL so that I could redirect the user to a website by typing this into the address bar:
http://mysite.com/goto/http://google.com
Use mod_rewrite and .htaccess to rewrite http://mysite.com/goto/http://google.com as http://mysite.com/goto/?url=http://google.com
RewriteEngine On
RewriteRule ^goto/(.+)$ /goto/?url=$1 [L]
Depending on your server configuration you may need to include a / in your rewrite path (i.e., ^/goto/(.+)$).
Unless you want to become a malware hub, I would wholeheartedly recommend you not doing this.
If you wish to allow redirect in such a manner, using http://mysite.com/goto/google and then work out the domain from a whitelist of available, allowed, destinations.
You will need to parse the data which could be a little tricky because you have to differentiate the difference between your URL and the other URL.
My suggestion is to not do so because the second that header is launched you will not see the url and it be better for you to just pass it as a get statement or a post.
EDIT
If you're determined then parse_url() is what you want. :)
#ide's method would work ... but you could also have the PHP script examine $_SERVER['PATH_INFO'], which is how that part of the URL would get passed to the CGI script.
(although, if there's a question mark in there, you'll also have to either make sure it's URI encoded, or also get the QUERY_STRING; you'll also lose any part after a hash, but you'd have the same problem with your current scheme)

Is there a way to exclude domain from link generation in wordpress

I have a website that responds on *.domain.com.
Going to x.domain.com or y.domain.com should produce the same web page.
What * is I do not know, but it is and important piece of info since we track things based on it.
When moving to wordpress we ran into a pretty severe problem. It seems to generate links (using get_page_link) with the domain which is set in the admin.
This will not work for us because we can't find a way to tell wordpress to generate links without the domain (why does it do this anyway?!) and every time a link is clicked the browser goes from: x.domain.com to domain.com (since domain.com is what we have in the admin).
Unfortunately WordPress is architected such that it is really hard to get rid of the domain component of URLs. But all is not lost! Read on as the answer to your question requires a bit of background.
The WordPress team made the decision to require the user of a site to hardcode the site domain either in the database via an admin console, which you can see in the following screen shot, of via PHP which we'll discuss below:
You might ask what the difference is between the the two URLs? Even I find it confusing because I almost never need anything other them to have them both set to the root URL and as it's not important for your question I'll just gloss over that detail. If you are interested though you can learn more here:
Changing The Site URL
Editing wp-config.php: WordPress Address (URL)
Giving WordPress Its Own Directory
Moving along, the other option is to hardcode the two PHP constants WP_SITEURL and WP_HOME in the /wp-config.php file which can be found in the root of a WordPress installation. Those two lines might look like this in your /wp-config.php file:
define('WP_HOST','http://domain.com');
define('WP_SITEURL','http://domain.com');
The good news is that you can define both of them dynamically based on the current domain your site is serving from (I'm going to assume you have both your DNS server and your Apache web server configured for wildcard DNS.) You can use the following code to match any subdomain name consisting of letters and numbers:
$root_domain = 'domain.com'; // Be sure to set this to your 2nd level domain!!!
$this_domain = $_SERVER['SERVER_NAME'];
if (!preg_match("#^([a-zA-Z0-9]+\.)?{$root_domain}$#",$this_domain)) {
echo "ERROR: The domain [$this_domain] is not a valid domain for this website.";
die();
} else {
define('WP_HOME',"http://{$this_domain}");
define('WP_SITEURL',"http://{$this_domain}");
}
The bad news is you may have some "artifacts" to deal with after you get it working such as how URLs are handled for image URLs stored in the database content (which may or may not end up being a problem) or for Google Maps API keys, etc. If you have trouble with them let me suggest you post another question here or even better at the new WordPress Answers Exchange also run by the same people as StackOverflow.
As for telling WordPress how to generate links, there are filters you can "hook" but in my quick testing I don't think you need it because WordPress will generate the links for whatever domain happens to be your current domain. Still if you do find you need them you can do it although be prepared to be overwhelmed by all the add_filter() statements required! Each one controls one of the different ways links can be generated in WordPress.
Here is the hook filter function and the 40+ add_filter() calls; you might not need them all but if you do here they are:
function multi_subdomain_permalink($permalink){
$root_domain = 'domain.com';
$this_domain = $_SERVER['SERVER_NAME'];
if (preg_match("#^([a-zA-Z0-9]+)\.?{$root_domain}$#",$this_domain,$match)) {
$permalink = str_replace("http://{$match[1]}.",'http://',$permalink);
}
return $permalink;
}
add_filter('page_link','multi_subdomain_permalink');
add_filter('post_link','multi_subdomain_permalink');
add_filter('term_link','multi_subdomain_permalink');
add_filter('tag_link','multi_subdomain_permalink');
add_filter('category_link','multi_subdomain_permalink');
add_filter('post_type_link','multi_subdomain_permalink');
add_filter('attachment_link','multi_subdomain_permalink');
add_filter('year_link','multi_subdomain_permalink');
add_filter('month_link','multi_subdomain_permalink');
add_filter('day_link','multi_subdomain_permalink');
add_filter('search_link','multi_subdomain_permalink');
add_filter('feed_link','multi_subdomain_permalink');
add_filter('post_comments_feed_link','multi_subdomain_permalink');
add_filter('author_feed_link','multi_subdomain_permalink');
add_filter('category_feed_link','multi_subdomain_permalink');
add_filter('taxonomy_feed_link','multi_subdomain_permalink');
add_filter('search_feed_link','multi_subdomain_permalink');
add_filter('get_edit_tag_link','multi_subdomain_permalink');
add_filter('get_edit_post_link','multi_subdomain_permalink');
add_filter('get_delete_post_link','multi_subdomain_permalink');
add_filter('get_edit_comment_link','multi_subdomain_permalink');
add_filter('get_edit_bookmark_link','multi_subdomain_permalink');
add_filter('index_rel_link','multi_subdomain_permalink');
add_filter('parent_post_rel_link','multi_subdomain_permalink');
add_filter('previous_post_rel_link','multi_subdomain_permalink');
add_filter('next_post_rel_link','multi_subdomain_permalink');
add_filter('start_post_rel_link','multi_subdomain_permalink');
add_filter('end_post_rel_link','multi_subdomain_permalink');
add_filter('previous_post_link','multi_subdomain_permalink');
add_filter('next_post_link','multi_subdomain_permalink');
add_filter('get_pagenum_link','multi_subdomain_permalink');
add_filter('get_comments_pagenum_link','multi_subdomain_permalink');
add_filter('shortcut_link','multi_subdomain_permalink');
add_filter('get_shortlink','multi_subdomain_permalink');
add_filter('home_url','multi_subdomain_permalink');
add_filter('site_url','multi_subdomain_permalink');
add_filter('admin_url','multi_subdomain_permalink');
add_filter('includes_url','multi_subdomain_permalink');
add_filter('content_url','multi_subdomain_permalink');
add_filter('plugins_url','multi_subdomain_permalink');
add_filter('network_site_url','multi_subdomain_permalink');
add_filter('network_home_url','multi_subdomain_permalink');
add_filter('network_admin_url','multi_subdomain_permalink');
While brings us to the final point. There is functionality in WordPress that attempts to ensure every URL that is loaded is served via its canonical URL which in general is a web best practice, especially if you are concerned with optimizing search engine results on Google and other search engines. In your case, however, if you really do not want WordPress to redirect to your canonical URL then you need to add a redirect_canonical filter hook and tell WordPress not to do it.
What follows is the code to make sure any page that serves as "x.domain.com" stays on "x.domain.com" even if all the URLs are filtered to be "domain.com". That may not be the exact logic you need but I'm just showing you the building blocks of WordPress so you'll be able to figure out the logic that you require.
A few final details about this function call; parameters #3 and #4 refer respectively to the priority (10 is standard priority so this hook will not be handled special) and the number of function arguments (the 2 arguments are $redirect_url and $requested_url.) The other thing to note is that returning false instead of a valid URL cancels the canonical redirect:
add_filter('redirect_canonical','multi_subdomain_redirect_canonical',10,2);
function multi_subdomain_redirect_canonical($redirect_url,$requested_url){
$redirect = parse_url($redirect_url);
$requested = parse_url($requested_url);
// If the path+query is the same for both URLs, Requested and Redirect, and
if ($redirect['path']+$redirect['query']==$requested['path']+$requested['query']) {
// If Requested URL is a subdomain of the Redirect URL
if (preg_match("#^([a-zA-Z0-9]+).{$redirect['host']}$#",$requested['host'])) {
$redirect_url = false; // Then cancel the redirect
}
}
return $redirect_url;
}
That's about it. Hope this helps.
-Mike
do you have any control over your hosting? Maybe you can use the rewrite module in apache, if you are using apache.
In httpd.conf add:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^x\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/x/$1
RewriteCond %{HTTP_HOST} ^y\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/y/$1
You can change the way you pass the variable "v" too
RewriteRule ^(.*)$ http://www.domain.com/$1&var=v
I didn't try the code, but i'm pretty sure that at least it will open your mind to a new way to deal with this issue --one that doesn't involve so much coding.
Cheers.
A.

Fix links addresses on websites HTML code

i have been working on one tool lately. It grabs all the link addresses from the website.
My problem is that links in html code sometimes is different:
/index.php
index.php
http://www.website.com/index.php
I need to make all links same:
/index.php -> http://www.website.com/index.php
index.php -> http://www.website.com/index.php
http://www.website.com/index.php -> http://www.website.com/index.php
Thanks for help.
Using preg_replace to fix relative urls
Requires:
$domain = the subject sites domain
$path = the document or string you are looking for relative links with in.
Returns:
$url = the doument or string with the links within it converted to proper urls with the domain given.
Code:
$url = preg_replace('<a\shref="([\/\?\w\.=\&]+)"([\s]rel="(\w+)")*>/', '<a href="http://{$site_domain}$1" rel="$3">' $path)
good luck, let me know how it goes.
Welcome to GoogleOverflow.com.
Here is the complete tutorial for parsing links in HTML using PHP and regex: http://www.the-art-of-web.com/php/parse-links/
Here's a function which will return the absolute URL given the base (current) URL and a relative one.
You need to check for the existence of a base tag. If you find it, it specify the base URL (otherwise, the base URL is the same path the browser points to, up to the last /).

Categories