Reputation: 3416
Right now, I am using a custom PHP solution to force WWW to be appended to my script URLs, since I am using WordPress' .htaccess rules to "clean up" index.php. The force WWW .htaccess rules are not compatible with WordPress' index.php cleanup rules.
With my script, you can browse to "http://scripturl.com/whatever" - my script then has a case for request URI "/whatever" and then does some action (or compiles the respective template) for that view.
I have added a series of checks prior to any any switches being called, which makes sure that the HTTP host from the REQUEST matches the HTTP host from a defined variable in my script. This enforces the addition of the "www".
My PROBLEM - is that (seemingly when only using IE) - when I enter a url, say "http://myscript.com/whatever", my script transforms the url to "http://www.myscript.com/whatever", as expected, and then redirects the header to the new URL. HOWEVER, if I change the URI request from "whatever" to "somethingelse", the page goes to "http://www.myscript.com/somethingelse" as expected, but for a brief second, "whatever" blinks in the url before the script redirects to "http://www.myscript.com/somethingelse".
To clarify: start with request "www.myscript.com/sam". request loads. Change /sam to /bob -> page changes to "www.myscript.com/bob" but "/sam" flashes in the url bar briefly before /bob loads.
It just doesn't feel "clean". I feel like my code might be doing an extra header jump or something. I have contrasted this to wordpress, by going to "www.wordpressurl.com/valid-page", then changing the URI to "www.wordpressurl.com/another-valid-page" - I don't see "/valid-page" flash in the URL bar when attempting to access "/another-valid-page", and vice versa.
Here is my code:
// Requested URL built from url in address bar
$requested_url = is_ssl() ? 'https://' : 'http://';
$requested_url .= $_SERVER['HTTP_HOST'];
$requested_url .= $_SERVER['REQUEST_URI'];
// Correct url built from predefined variable
$correct_url = is_ssl() ? 'https://' : 'http://';
// "Correct" script url
$user_home = @parse_url('http://www.myscript.com');
if ( !empty($user_home['host']) )
$correct_url .= $user_home['host'];
else {
die('malformed url');
}
$correct_url .= $_SERVER['REQUEST_URI'];
// If URL in address bar is not proper, perform redirect (preserve URI)
if ($correct_url != $requested_url) {
hc_redirect($correct_url, 301);
}
// Get page from request, handle accordingly
$page = $_SERVER['REQUEST_URI'];
switch ($page) {
/* Index */
case '/':
echo "Index queried <br />";
break;
...
Why does the old URI flash in the navbar before the new URI is loaded (from the redirect)? Like I said, this only appears to happen in IE - but WordPress does not have this same behavior (in IE or any other browser), so I know there must be something wrong with my code, an "extra step" which is happening without my knowledge. I'm somewhat new to PHP.
Any thoughts?
Edit: hc_redirect, and other used funcs: http://pastebin.com/fVNEckEg
Upvotes: 0
Views: 703
Reputation: 9957
Your script continues to output data after you redirect, the browser waits for the data and only then redirects. If you don't need to continue execution of the script after you send the redirect header, stop the script's execution. For example:
// If URL in address bar is not proper, perform redirect (preserve URI)
if ($correct_url != $requested_url) {
hc_redirect($correct_url, 301);
die();
}
Upvotes: 2