Learning MVC .. need to understand htaccess.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Learning MVC .. need to understand htaccess.

Post by gautamz07 »

Hey guys i have a questiion about the htaccess file and it is specific to a little demo project that i am doing ,

have a look below , i have the following directory structure :

App
- htaccess file
- controllers
- home
- core
- app
- controller
- modals
- Users
- views
- index.php
- home
- init.php

Public
- CSS
- index.php
- htaccess file


now inside the htaccess file in public , i have the following commands :

Code: Select all

Options -MultiViews
ReWriteEngine On

RewriteBase /mvc_model/public


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


RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

:banghead: :banghead:

can somebody thoroughly explain to me what the above commands are doing ? ld please don't re direct me to another URL , i would prefer a human explanation .

i appreciate your time and patience .

gautam.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Learning MVC .. need to understand htaccess.

Post by Celauran »

mvc_model, I'm assuming, is your app's parent directory (i.e. App and Public above are both subdirectories of mvc_model). The RewriteBase directive is saying that all rewrite rules should be relative to that directory.

Code: Select all

RewriteCond %{REQUEST_FILENAME}  !-d
If the file requested isn't a directory

Code: Select all

RewriteCond %{REQUEST_FILENAME}  !-f
And the filename isn't a file

Then apply the following rewrite rule

Code: Select all

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
This rule says to start at the beginning of the URI (^), capture any character (.) any number of times (+) until the end of the URI ($), and pass that as a query string to index.php. Do not process any other rewrite rules (L), and include any query string that was on the original URI (QSA).

index.php probably loads your autoloader, does some basic bootstrapping, and then dispatches the request as per your routing rules.
Post Reply