Mobile app version of vmapp.org
Login or Join
Berryessa370

: How can I pass optional URL parameters through my rewrite rule? I created a CMS that identifies if the GET value of url is defined, and if it is, it returns the html contents of the page

@Berryessa370

Posted in: #Htaccess #ModRewrite #Php

I created a CMS that identifies if the GET value of url is defined, and if it is, it returns the html contents of the page from the frontend template on index.php. This ideally is to make a templating system that people who don't know much PHP can use, while being able to expand on it if they ever do know PHP.

It all worked fine with this htaccess. The following htaccess converts localhost/index.php?url=(page) to localhost/(page).
RewriteEngine On

RewriteRule ^(|/)$ index.php?url=
RewriteRule ^([a-zA-Z0-9_-]+)(|/)$ index.php?url=


However, I expanded the CMS to identify if the GET url end is set. If it's value is backend, and the value of $_GET['url'] isn't defined, it assumes that the value is index. If the value is defined, it uses the value to fetch that file. I now want my htaccess to do the following:


If the first parameter is backend then make the url from localhost/index.php?end=backend&url=manage-pages to localhost/backend/manage-pages. If it is anything but backend convert it from localhost/index.php?url=(page) to localhost/(page).

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Berryessa370

1 Comments

Sorted by latest first Latest Oldest Best

 

@Heady270

You need to use the QSA flag on your rewrite rule. It preserves any query string from the original URL and appends it to the new URL. Your rewrite rule would be:

RewriteRule ^(|/)$ index.php?url= [QSA]
RewriteRule ^([a-zA-Z0-9_-]+)(|/)$ index.php?url= [QSA]

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme