Posted By

FatFolderDesigner on 08/11/12


php htaccess apache rewrite

Versions (?)

Who likes this?

2 people have marked this snippet as a favorite


Perfect PHP .htaccess rewrites

 / Published in: PHP


These two snippets together, the first in the .htaccess file, and the second in your PHP create an elegantly simple solution to arbitrary URLs with php. For a breakdown and example check out the link.

Don't want to follow the link, how about the quick version. The .htaccess redirects anything that doesn't exist to the PHP, where the script breaks it apart and places it all in the $_urlvars for your own uses.

  1. /* The .htaccess part */
  2. Options +FollowSymLinks
  3. RewriteEngine On
  4. RewriteCond %{REQUEST_FILENAME} !-d
  5. RewriteCond %{REQUEST_FILENAME} !-f
  6. RewriteCond %{REQUEST_FILENAME} !-l
  7. RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
  9. /* The PHP part */
  10. $_urlvars = explode('/',$_REQUEST['url']);
  11. $_urlvars['_'.$k] = $v;
  12. foreach($_urlvars as $k => $v){
  13. $_urlvars[$v] = (isset($_urlvars[$k+1])?$_urlvars[$k+1]:'');
  14. }

Report this snippet  

You need to login to post a comment.