drupal prepending 'node' to the link url - php

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.

Related

Dynamic links linking to root domain

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.

How can I create a URL rewrite similar to Wordpress in my own PHP app

I am working with PHP5.3.6 on Windows 2008R2.
In Wordpress, I can set all kinds of friendly URLs which ultimately route to one PHP page. From the outside, it looks like many pages, but is only one.
In the web.config file (or .htaccess) file, there is only one instruction as opposed to having one entry per page/article.
This means that somewhere PHP is looking at the URL, comparing it to the database (where the article titles exist) and then routing transparently to the page.
All of this is transparent to the end user.
How is this accomplished in a non wordpress PHP site?
Here's a related answer that touches on how to do that. In brief, you'll want to check the $_SERVER['REQUEST_URI'] and just parse that.
Here's a simple example of parsing the request (MVC routers are usually configurable, and can route and match for many different URI structures):
If your format is something like news/article-slig you can do this (example code, there are less rigid ways to do this):
list($section, $slug) = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
At this point your PHP script knows how to interpret the request. If this were a full blown MVC application, the router would load the matching controller and pass the request data to it. If you're just doing a simple single page script, loading some data, then your DB calls would follow.
If the request is invalid, then a simple header() call can notify the browser:
header('HTTP/1.0 404 Not Found');
And any data output would be the content of your 404 page.
I can't vouch for wordpress, one method I have used is to redirect 404's in .htaccess to index.php and then have that file sort by parsing:
$sub = $_SERVER['SERVER_NAME'];
$file = $_SERVER['REQUEST_URI'];
$sub can be used to mask non existant subdomains to a specific file. $file can be used in a switch or if clause to include / redirect based on file name.
Obviously, you need to make sure that the alias' are not actual files in your doc root.
Its called Routing(you can also check info about Front Controller pattern). You can write your own implementation by redirecting all your requests to single file using server settings and parse request in this file. You can also check, for example, Zend_Controller_Router docs and sources to understand how it works.
http://framework.zend.com/manual/en/zend.controller.router.html
http://framework.zend.com/manual/en/zend.controller.html

URL problems with swfAddress, mod_rewrite and SEO

Hey guys, I've got a full Flash website that's deeplinked and I'm working on a php script that will dynamically generate the alternate content on the page.
I can't get the SEO version of swfaddress to work so I'm trying to go around another way.
This is how it works so far - my crawlable links are like this:
mydomain.com?id=video -----> is rewritten as: mydomain.com/video/
The php script reads the id and prepares the appropriate content. Problem is, when users click on the flash content, their urls appear like this:
mydomain.com/video/#contact,
mydomain.com/video/#about-us etc etc
Which is confusing. Is there a way to get rid of "/video/" in the uri path with PHP or javascript? Any ideas or not possible?
To anyone who may be in the same position as me; struggling to understand Asual's swfAddress SEO solution I've reached a satisfactory conclusion.
I've used a single index.php and supporting datasource.php which contains all results in a switch statement. The switch statement is called first in the index.php and runs through collecting the following info from an id (eg. ?id=video) sent via GET. The switch statement then continues to match up appropriate content, setting variables for the rest of the script to use. I've used the following:
$title
$description
$keywords
$canonical // hint: -> 'domain/video?id=somevid' to 'domain.com/video/' (see canonical in google)
$stylesheet
$body
After the datasource.php, I include a "head.php" below it, which gets the html rolling, doctype etc (but is mostly concerned with the < head >) and lays out the keywords, description and content, specific to that page.
Swfaddress javascript will set the deeplinking upon entry for Flash users:
SWFAddress.setValue(< ?php echo "/$id" ?>); // eg."/video"
The index.php uses swfobject to embed - I use static embedding (I heard somewhere it was better for SE, more future proof but who knows, I prefer it though). The alternate content is a div, which echos the stored $body extracted earlier.
Finally I use mod_rewrite to setup my crawlable links. During testing I'm referencing my files directly.
< IfModule mod_rewrite.c >
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^home/? ?id=home [NE,L]
RewriteRule ^about/? ?id=about [NE,L]
RewriteRule ^video/? ?id=video [NE,L]
< /ifModule >
This ensures my links look like this: http://www.mydomain.com/video/
Which results in: http://mydomain.com?id=video
A search engine can crawl it perfectly, viewing only the html content where as users will see the flash, and when they click on the links they will see something like this:
http://www.mydomain.com/video/#/contact
As I said it's satisfactory, not as ideal as Asual's SEO solution, but clean enough. I couldn't find ANY information on swfaddress' SEO sample anywhere, and I certainly don't know enough php to dive into the vast uncommented code.
Oh, be sure to include this in the head of your index.php, it will ensure your links don't screw up when your swfs load external files.
< base href="http://www.mydomain.com/" />
Hope that helps someone at some point.

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.

set default on webpage

I have an issue with url direction here. I have used mod_rewrite in apache to rewrite the url from domain.com/page.php to domain.com/path/page.php.
I have some link in the webpage for example href="newpage.php" will automatically go to domain.com/path/newpage.php instead of domain.com/newpage.php. May I know is there any php method to set all the default url in the php file itself to avoid this path issue? without using any variable like href="<?php echo $_SERVER['domain']; ?>newpage.php"
If you know how many folders deep the file is you could just backtrack by doing ../newpage.php. So if you're at domain.com/folder/currentpage.php you could do ../newpage.php. To get to domain.com/newpage.php.

Categories