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.
Related
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.
I've been wandering for a while trying to get out of this problem.
I have a php software in which I want to know where the script is being executed.
e.g.
I have /foo/bar/home.php . In this case I would like to know that /foo/bar is my root. But if I have an example admin page /foo/bar/admin/index.php I would like to have /foo/bar in this case too.
Or
/foo/bar/foo/index.php -> /foo/bar/foo
/foo/bar/foo/randomname/home.php -> /foo/bar/foo
How can I accomplish that ?
Thanks for help
Because /foo/bar does not really sound like a filesystem path to me, I assume that this is the first part of the URL path.
How does the application know that this prefix is applied to everything? It must be installed somewhere, and if no rewriting is applied, the filesystem path layout and url path layout match on some level. This might be used to actually generate path information by subtracting some strings, but I doubt it's usefulness.
I'd opt for defining a constant "INSTALL_PATH" and/or "INSTALL_URL" in a file that is included everywhere, which knows its relative location to the base url or file path, and does a simple string operation:
define('INSTALL_PATH', basename(__DIR__); // go one level up
You can access the properties of the $_SERVER superglobal variable.
echo $_SERVER['DOCUMENT_ROOT'];
The following, if located in "/foo/bar/foo/index.php", would echo /foo/bar/foo/.
I want both index.php included, and index.php excluded from my URIs to work smoothly on my site. I need it because of uploadify not working, and it is giving me a HTTP error.
It works if I have changed this
$config['uri_protocol'] = 'AUTO';
to this
$config['uri_protocol'] = 'QUERY_STRING';
, but then whole system is not working properly.
For your assets, first, on config.php set the $config['base_url'] to your website URL.
Then, when linking to assets on which you do NOT want index.php, just use the function base_url
<?php echo base_url("assets/uploadify/uploadify.swf"); ?>
// Generates http://URL/assets/uploadify/uploadify.swf
Follow the link in the comment from Indranil above to remove index.php from your URIs by modifying your .htaccess file.
Here is the address again for reference:
http://codeigniter.com/user_guide/general/urls.html
Keep in mind too that this can vary from host to host in the way that it needs to be formatted, so follow up on that with your hosting service. Most usually have guides that show you how to configure this properly.
I'd also like to know how you're using your links, but I'll go ahead and explain what I think may be happening to you. If you're doing any kind of link/anchor or redirect() using the URL Helper in CodeIgniter, then make sure you're using the right function to execute those actions. Here's what I mean:
The site_url() function/method will always include index.php in your URIs. Here's an example using the site name http://example.com as your website's domain.
<?php echo site_url('home') ?> will turn into http://example.com/index.php/home
While base_url() function/method will always exclude index.php unless you include it. Here's an example using the site name http://example.com as your website's domain.
<?php echo base_url('home') ?> will turn into http://example.com/home
and
<?php echo base_url('index.php/home') ?> will turn into http://example.com/index.php/home
Keep in mind that using these functions/methods are only effective AFTER you have properly modified your .htaccess file.
Here is the link that shows you a more in-depth difference between the site_url() and base_url() functions.
http://codeigniter.com/user_guide/helpers/url_helper.html
I hope this helps!
How can I alter url or part of it using php? I lost the code that I have used and now I cannot find answer online.
I know using header('Location: www.site.com') gets redirect but how can I just show fake URL?
I don't want to use mod_rewrite now.
It is impossible in PHP, which is executed server side. Any change to the url you make will trigger a page loading.
I think it may be possible in javascript, but I really doubt this is a good idea, if you want to rewrite an url only in the user adressbar, you're doing something wrong, or bad ;)
What you've actually asked for isn't possible in using PHP (Although, in JavaScript you can use the dreadful hashbang or the poorly supported bleeding edge pushState).
However, in a comment on another answer you stated that your goal is actually friendly URIs without mod_rewrite. This isn't about showing a different URI to the real one, but about making a URI that isn't based on a simple set of files and directories.
This is usually achieved (in PHP-land) with mod_rewrite, so you should probably stick with that approach.
However, you can do it using ScriptAlias (assuming you use Apache, other webservers may have different approaches).
e.g. ScriptAlias /example /var/www/example.php in the Apache configuration.
Then in the PHP script you can read $_SERVER['REQUEST_URI'] to find out what is requested and pull in the appropriate content.
You can make somewhat SEO-friendly URLs by adding directories after the php script name so that your URLs become:
http://yoursite.com/script.php/arg1/arg2
In script.php:
<?php
$args = preg_split('#/#', $_SERVER['PATH_INFO']);
echo "arg1 = ".$args[1].", arg2 = ".$args[2]."\n";
?>
if you use some more htaccess trickery, then you can make the script.php look like something else (see #David's answer for an idea)
You can try using,
file_get_contents("https://website.com");
This is not going to redirect but fire the api and you can catch the output by assigning a variable to above function.
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)