Perfect PHP .htaccess rewrites


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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]
  8.  
  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. }

URL: http://fatfolderdesign.com/653/php/perfect-php-htaccess-rewrites

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.