I developed a small custom MVC framework for my website. it's index.php check $_POST['page'] & $_POST['subpage'] and includes related php file for specified parameter.
Ex: index.php?page=foods (this includes view/foods.php)
Ex: index.php?page=foods&subpage=pizza (this includes view/foods/pizza.php)
Now I want to clean my URLS using .htaccess. I tried few methods to pass second parameter. But I failed. This works fine...
RewriteEngine on
RewriteRule ^([^/\.]+)?$ index.php?page=$1 [L]
But when i tried to pass second parameter its not working fine. This one change css/js/iamges path.
RewriteEngine on
RewriteRule ^([^/\.]+)?$ index.php?page=$1
RewriteRule ^([^/\.]+)/([^/\.]+)?$ index.php?page=$1&sub=$2
If you use the QSA (Query String Apend) flag, you can simply pass the entire request string to an index file -
QSA - Appends any query string from the original request URL to any query string created in the rewrite target
So your rule would look similar to this -
RewriteRule %{REQUEST_FILENAME} !-d
RewriteRule %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?uri=$1 [QSA,L]
In your index file you can .explode()
by slashes -
$splitArr = explode('/',$_GET['uri']);
And/or use the built in parse_url()
function - http://www.php.net/manual/en/function.parse-url.php
parse_url
— Parse a URL and return its components
Most url-rewriters of this type explicitly exclude files/directories that actually exist:
RewriteRule %{REQUEST_FILENAME} !-d
RewriteRule %{REQUEST_FILENAME} !-f
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteRule ^public/(js|css|images)(/.*|$) - [L]
RewriteRule ^(.+)$ index.php?page=$1 [QSA,L]
Here is how finally I've done it !!!