views:

17

answers:

3

Hi all,

I'm making a web app and I'm checking for the presence of a configuration file to determine whether or not to run the installation script. However, since it is a web app, I have no idea at what URL this script will be stored.

Is there any way to redirect a page, either using PHP or HTML, to a relative file?

Thanks in advance!

A: 

in PHP you can use header('Location: http://www.example.com/'); but it must be used with full url, not the relative one, so you just need to build full url from your relative url and use header

vittore
Untrue. Actually, I just found the solution, read in my answer.
esqew
also you can do it on client side using javascript, but if you can figure out that redirect is needed on server side , better to do it on server side
vittore
A: 

I found that I could just do this:

header('Location: ./install.php')

esqew
Many (most?) modern browsers correctly handle relative URIs in the Location header, but it is against the HTTP/1.1 spec (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30)
bd808
+1  A: 

You can reconstruct the URL of the currently executing script using the 'SCRIPT_NAME' variable set by mod_php. From there it's just a matter of relative path manipulation to construct the absolute URL of the script you want to redirect to.

<?php
$scheme = $_SERVER['HTTPS'] ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'];
$basedir = dirname($_SERVER['SCRIPT_NAME']);
header("Location: {$scheme}://{$host}{$basedir}/redirect_target_page.php");
bd808