Search code examples
phpwordpresspermalinks

How to enable PHP Scripts with Folderpath like Wordpress or Zend?


How can i archieve permalink urls with php scripts? I mean something like wordpress does for it posts like

url: www.example.com/year/entry 

$year = ...
$entry = ...

without having the actual folder created ... I dont want to use .htacess because Wordpress does not use it either i think.

Normal behaviour would be a 404 Error because the folder does not exist.


Solution

  • Use mod rewrite in Apache (different servers will allow different rewriting mechanisms).

    http://httpd.apache.org/docs/current/mod/mod_rewrite.html

    Most of the time the rewrites are done using regex. Here is an example (i will explain what it does after) that you put in a .htaccess file in the root directory of your site ie: http://website.com/

    RewriteEngine on
    RewriteRule ^js/main.js$ /scripts/main.php [L]
    

    That will allow you to enter the following in your browser: http://website.com/main.js But the file will actually be located on the server at http://website.com/scripts/main.php and the files can be located OUT of the public directory for better security.

    Another example but this time using a little more regex

    RewriteEngine on
    RewriteRule ^js/([0-9a-f]+).js$ /scripts/main.php [L]
    

    Allows all these to go to the same file:

    will all load http://website.com/scripts/main.php

    P.S. The [L] indicates to the server that it's the LAST rewrite and it does not need to keep looking for matches.