Hi I'm trying to create a really simple rout for my database driven website. Wold be great if someone could help.
My dynamic link is the following: /views/page.php?page_slug=link-one
As you can see below I was hoping my dynamic link would would but it doesn't. I get an error saying that the file/directory doesn't exist. I guess the question is how would I get a dynamic URL in there (variable) so that it re-routes? I might be going completely the wrong way around this.
Here is my htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php
index.php file below.
<?php
$request = $_SERVER['REQUEST_URI'];
switch ($request) {
case '':
case '/':
require __DIR__ . '/views/index.php';
break;
case '/link-one':
require __DIR__ . '/views/page.php?page_slug=link-one';
break;
}
Any help much appriciated.
CodePudding user response:
Try removing the query from the request uri.
And you can't stick query parameters in the path to an included file. But you can populate the get parameter if you want the included script to behave like it was submitted. But from the looks of it I think you should avoid that.
$request = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
switch ($request) {
case '/':
require __DIR__ . '/views/index.php';
break;
case '/link-one':
$_GET['page_slug'] = 'link-one';
require __DIR__ . '/views/page.php';
break;
}
And maybe this .htaccess is better in the long run where query string is appended to the route. And this is the last rule not conflicting others that may follow:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]