How To $_GET['id'] From A Clean Url Like "example.com/products/123/title-of-this-product"?
I'd like my URL to look like this:
The actual URL is this:
.htaccess file correctly interprets my URLs so that users who are on this page only see the clean URL. However, if I try to use
$_GET['id'] on the
products.php page, the script crashes because it doesn't recognize any
id in the URL.
Options +FollowSymLinks +MultiViews RewriteEngine On RewriteRule ^([0-9]+)(?:/([^/]*))?/?$ ./products.php?id=$1&title=$2 [L,NC]
$product_id = isset($_GET['id']) ? $_GET['id'] : "Error! I can't find your product!";
How can I retain the URL parameters for PHP functions if I want a clean URL?
You actually have 2 problems here...
MultiViews(part of mod_negotiation) is enabled and it's this that is serving
RewriteRulepattern is incorrect and won't match the requested URL.
Options +FollowSymLinks +MultiViews
You need to disable
MultiViews (you have explicitly enabled it). It is MultiViews (part of mod_negotiation) that is serving your
products.php file (without any URL parameters), not the mod_rewrite directive that follows. MultiViews essentially allows extensionless URLs with minimal effort, however, it can be the cause of unexpected conflicts (with mod_rewrite) - as in this case.
RewriteRule directive is not actually doing anything. If the
.htaccess file is located in the document root then the
^([0-9]+)(?:/([^/]*))?/?$ does not match the requested URL (
/products/123/title-of-this-product), so the directive is not actually processed at all (although MultiViews would still override this even if it did).
Try it like this instead:
# Disable MultiViews Options +FollowSymLinks -MultiViews RewriteEngine On RewriteRule ^products/([0-9]+)(?:/([^/]*))?/?$ products.php?id=$1&title=$2 [L,NC]
You were missing
products from the start of the URL-path matched by the
products/ at the start of the regex it would only match if you are in a
/products/ subdirectory, ie.
RewriteRule directive matches relative to the location of the
.htaccess file itself.
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?