I am experiencing an odd problem with URLs. I'm using .htaccess and header(location) to set the web address.
I think that I'm "resetting" the web address at the bottom with header (location), but instead it looks like the address that I'm specifying gets appended to the current address rather than replacing it all.
My code is:
$route = ltrim(strtok($_SERVER['REQUEST_URI'], '?'), '/');
if($route == strtolower($route)) {
if($route === 'contact/edit') {
include __DIR__ . '/../myscripts/MainScript.php';
$driver = new MainDriver();
$page = $driver->edit();
}elseif($route === 'contact/add' ) {
include __DIR__ . '/../myscripts/MainScript.php';
$driver = new MainDriver();
$page = $driver->add();
}else {
include __DIR__ . '/../myscripts/MainScript.php';
$driver = new MainDriver();
$page = $driver->home();
}
}else {
http_response_code(301);
header('location: index.php?route=' . strtolower($route));
}
.htaccess looks like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [NC,L,QSA]
RewriteRule ^/contact/edit(new|existing)$ /index.php?route=/contact/edit$1 [NC,L,QSA]
localhost opens just fine. But, if I try to go to localhost/contact/editNew, instead of taking me to localhost/contact/editnew I am taken to localhost/contact/index.php?contact/editnew and the welcome page index.php gets loaded!
I think the last rewrite rule is what's going to have to be modified, but I don't know for certain. That's why I'm looking for help here. I wrote that rule based on a mod_rewrite page on apache.org that I read.
Let me know if you need more details to understand this issue.
(EDIT#2)
I have edited the header call to
header('location: ' . strtolower($route);
And I still obtain the same result, which confirms that the problem is caused by the URLRewrite rule that I'm learning now. Do you folks know how to fix this?