2

I am creating a custom PHP MVC pattern from scratch. This is how my url looks like

http://domain.com/post/create

I could achieve this with the following htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+) index.php?controller=$1&action=$2 [L]

So, when I print my $_GET, I am getting this

Array ( [controller] => post [action] => create )

Up to this is fine. But my issue is, if I have any extra parameter in query string, I am not able to get that.

ie. if I print $_GET for http://domain.com/post/create?id=1, I am not getting id.

Any help would be greatly appreciated

tereško
  • 58,060
  • 25
  • 98
  • 150
laradev
  • 868
  • 1
  • 10
  • 22

1 Answers1

3

This is exactly where the QSA flag is created for. By default, if you don't specify a query string, it will re-use the query string of the original request. If you specify a query string, it will overwrite it. If you use the QSA or qsappend (query string append) flag, it will append the original query string to the new query string. See this documentation document for more information.

Your rule would end up looking like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+) index.php?controller=$1&action=$2 [QSA,L]
Sumurai8
  • 20,333
  • 11
  • 66
  • 100