Magento 1.9 - How to rewrite magento cms page to custom url with query parameter
-
11-05-2021 - |
Question
I want to rewrite CMS page url to custom url.
For eg. CMS page url is "/result/?q=test"
and rewrite url should be "/demo/test"
For this I'm trying to below htaccess rule
RewriteRule ^demo/?$ /result/?q=$1 [QSA,L]
Url "/result/?q=test"
is working, but the url "/demo/test"
is showing 404 not found.
What rule should I use this?
Solution
Actually all request redirected to the index.php which internally uses $_SERVER['REQUEST_URI'] to determine what page or resource actually requested. Above htaccess will just redirect the request to /result/ but to index.php $_SERVER['REQUEST_URI'] will remain /demo/test
What you can do is add below code to your index.php it will do the trick
if(strpos($_SERVER['REQUEST_URI'],'/demo/')!==FALSE)
{
$_SERVER['REQUEST_URI'] = '/result/?q='.str_replace('/demo/', '', $_SERVER['REQUEST_URI']);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange