Couple of questions about .htaccess and friendly urls Couple of questions about .htaccess and friendly urls apache apache

Couple of questions about .htaccess and friendly urls


To answer your first question:You could use RewriteRule ^([a-zA-Z]{2})([/]?)(.*)$ path/file.php?language=$1This limits the first string to two characters and passes it on to $_GET['language']

Edit: adding RewriteCond %{REQUEST_FILENAME} !-fand RewriteCond %{REQUEST_FILENAME} !-d will prevent conflicts with existing directories / files

Second question is much more difficult..

Update:

What Shad and toopay say is a good start in my opinion.Using explode() to seperate levels and comparing it to the slug is quite simple.But it's getting complicated once you want to add flexibility to the script.

function get_URL_items() {$get_URL_items_url = $_SERVER['REQUEST_URI'];$get_URL_items_vars = explode("/",$get_URL_items_url);for ($get_URL_items_i = 0; $get_URL_items_i < count($get_URL_items_vars) ; $get_URL_items_i++) {    if(strlen(trim($get_URL_items_vars[$get_URL_items_i])) == 0) {        unset($get_URL_items_vars[$get_URL_items_i]);    }}return $get_URL_items_vars;

Let's say you you've got a website with a sub-section called "Festival" and a database filled with info for 100+ artist and you want your URLs to look like website.com/festival/<artistgenre>/<artistname>/. You don't want to create 100+ pages in your CMS so <artistgenre> and <artistname> are some kind of wildcards.

I found it hard to achieve this without a lot of if/else statements like:

$item = get_URL_items();    if(is_user($item[2]) && is_genre($item[1]) && is_festival($item[0])) {  // do mysql stuff here}


If I were you, I would use something like this:

.htaccess:

Options +FollowSymLinksRewriteEngine OnRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME} !/main.php$RewriteRule ^([a-zA-Z]{2})?(.*)$ main.php?lang=$1&path=$2 [L,QSA]

main.php:

$langs = array('en','de','ru'); // list of supported languages$currentLang = isset($_GET['lang'])&&in_array($_GET['lang']) ? $_GET['lang'] : $defaultLang; // current selected language$path = $_GET['path']; // current path

Then, in main.php you may parser path according to your needs


In answer to your bounty question I would use this:

RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^([A-Z]{2}\/)*(([A-Z]+\/)*)([A-Z]+)\.html$ index.php?lang=$1&parents=$2&pagelink=$4 [NC,QSA,L]

Since you want to be able to handle any number of generations/levels in your URL, have you thought about how you want to catch them in you PHP script?

You definitely don't want to be going and checking isset($_GET['parent1']);isset($_GET['parent2']) etc etc etc.

As some of the other responses have indicated, you really need to be parsing your URL inside your PHP script; to that end, my RewriteRule hands off the entire 'parents' section of the URL, which your script can then explode and parse; but doesn't interfere with normal no-parent urls. =)