I am having problem with white-spaces in URL. How to correctly redirect request if the address contains a space.I am using xampp,this is what i have so far.
htaccess
RewriteEngine On
RewriteBase /test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]
index.php
//sample pages
$pages = array('home','about','contact');
$parts = explode("/",$_SERVER['REQUEST_URI']);
if(!in_array(end($parts), $pages))
{
header("HTTP/1.0 404 Not Found");
echo "Page not found";
die();
}
else
{
echo "You are on the ".end($parts)." page";
}
error.php
echo "Error!";
When i type address http://localhost/test/contactk or http://localhost/test/contact everything is working correctly, but if someone by accident, type an address including whitespace everything crash the page is redirected outside localhost and I can see something like this:
Showing results for localhost/test/contact
Search instead for localhost/test/cont act
How to redirect all of bad request to error.php file
Thank You.
Showing results for localhost/test/contact Search instead for localhost/test/cont act [this is not a link]
This sounds like your browser is interpreting your URL as a search query when you accidentally put a space in it. It seems to then be redirecting you to the search engine instead of your web site.
Because this is your browser's doing and not your server's, there is nothing you can do to change this behaviour globally. You can of course change your browser's configuration but that's it'll still happen to everyone else with the same browser.
That's a typical browser feature. You have a location bar where you can do two entirely different tasks:
You are typing a string that has not been identified as URL for two reasons:
http://
)... so the browser assumes you want to google it, just as if you'd typed "restaurants in barcelona".
To be on the safe side you can just type the complete URL:
http://localhost/test/cont%20act
... but you cannot prevent your visitors from making this error. This is something private between browser and user ;-)