twitterfacebookgoogle plusrss feed

Pages

Showing posts with label .htaccess. Show all posts
Showing posts with label .htaccess. Show all posts

Tuesday, March 18, 2014

.htaccess for MVC


In MVC routes are normally sent through query string formatted using .htaccess. You are passing a query string to index.php i.e. domain.com/index.php?route=records/dvds/10 transform into domain.com/records/dvds/10. What you have to do is create a new file .htaccess using any supported editor (netbeans will be best, remember notepad of windows do not support a file with no file name) and put following lines into the file, save and place this file in root of the folder where you want to create operational MVC.


RewriteEngine On

RewriteCond %{REQUEST_FILENAME}% !-d
RewriteCond %{REQUEST_FILENAME}% !-f
RewriteCond %{REQUEST_FILENAME}% !-l

RewriteRule ^(.*)$ index.php?route=$1


It will successfully tranforme junky query string into nice looking url. The first part of url is considered a controller or class, the second part after front slash "/" is known to be method and the last one is parameters for the method. If "route" in query string is records/dvds/10 (looks like domain.com/records/dvds/10) you have to explode this string on "/" and it will returns an array contains three pockets, 0=>records (controller or class), 1=>dvds (method of records), 2=>10 (parameter of records).

Monday, March 17, 2014

QSA or Query String Append



QSA is flag used in .htaccess to append query string in traditional format into formatted or over written url after the URI has been rewritten. For example you are rewrite url from http://www.example.com/index.php?id=foo&user=bar to http://www.example.com/foo/bar and now you want to pass traditional query string to this over written url like http://www.example.com/foo/bar/?action=success then you have to declare following rule in .htaccess like RewriteRule ^([A-Za-z0-9]+)\/([A-Za-z0-9]+)$ index.php?id=$1&user=$2 [QSA]