URL routing in PHP

I am working on a webpage project. I decided to use Apache, PHP (5.1.7, the version imposed by my service provider) and Dwoo (templating) for this.

I want to redirect urls to my templates. I know that there are many frameworks that do such things. I'm just wondering if there is a good way to achieve this.

I created my project as follows:

  • src / dwoo - Dwoo files
  • index.php - This should handle routing. Currently, it simply displays the first page of the site using the template.
  • templates - Templates that represent actual pages.

There is a minimum amount of business logic (no real model). All these are just pretty static pages. Using templates simplifies maintenance work (inheritance, i.e.).

Any idea how to configure routing in this case? I assume that ideally, each given URL should route through index.php, which then somehow decides which template to render (i.e. / Category / pagename will appear in the / category / pagename.tpl templates).

+8
php url-rewriting url-routing apache dwoo
source share
3 answers

Use mod_rewrite to route just one index.php file. Then check the variable in $_SERVER['REQUEST_URI'] in this file to send the required handler.

This configuration will enable mod_rewrite if installed:

 DirectorySlash Off Options FollowSymLinks Indexes DirectoryIndex index.php RewriteEngine on RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [L] RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^.*$ - [L] RewriteRule ^.*$ index.php [L] 
+10
source share

Like trolskn (+1), it describes:

Use mod_rewrite to route just one index.php file. Then check the variable in $_SERVER['REQUEST_URI'] in this file to send the required handler.

But I found the following .htaccess (placed in a folder with index.php , which should "consume" everything after it) is much more useful:

 Options -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [QSA,L] 

I would also like to note that you may come across a message

 .htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration 

This is easily solved with sudo a2enmod rewrite && sudo service apache2 restart ( source )

+2
source share

You might want to use PEAR Net_URL_Mapper .

+1
source share

All Articles