Home > Back-end >  How to Route Php Files Using .htaccess
How to Route Php Files Using .htaccess

Time:02-26

I have three folders--Model, Views and Controller--and I have stored the index.php file in the Views folder. I am not able to route the files. I tried the following code in the .htaccess file

RewiteEngine On
RewiteCond %{REQUEST_FILENAME} !-f
ReWriteRule ^(.*)$ index.php

CodePudding user response:

Let's assume your project structure is something like below:

Model
Controller
Views -> index.php

You can use this .htaccess file in the root folder:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteRule ^$ view/ [L]
  RewriteRule (.*) Views/$1 [L]
</IfModule>

This sends all the traffic to Views folder.

And another this .htaccess file inside the Views folder:

<IfModule mod_rewrite.c>
  Options -Multiviews
  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>

This sends all the traffic throw index.php file with a pretty URL.

Tips:

But I suggest changing your structure as below that seems to be better:

public -> index.php #will include bootstrap.php
          assets
          .htacess

app --->> Model
          Controller
          View
          Helper
          bootstrap.php #contains autoload and includes
          ...
.htaccess

So all traffic will pass throw public/index.php and the application folder and services are separate from public and assets.

  • Related