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 http://localhost/index.php?url=(page) to http://localhost/(page).

RewriteEngine On

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

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:

  1. If the first parameter is backend then make the url from http://localhost/index.php?end=backend&url=manage-pages to http://localhost/backend/manage-pages.

  2. If it is anything but backend convert it from http://localhost/index.php?url=(page) to http://localhost/(page).


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=$1 [QSA]
RewriteRule ^([a-zA-Z0-9_-]+)(|/)$ index.php?url=$1 [QSA]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.