Friday, June 8, 2012

htaccess rewrite on urls without common pattern


In a new project, a website has a set of static html pages. The owner needs to convert them to dynamic ones. Each page has: a text section, a small photo plus some links towards other pages, related to this one.



All these i can put in database and create a dynamic page (php).



My problem is that each static page is unique - no common pattern in url. And there 860 such pages at the moment. I can have one url like:




folder1/folder2/item1.htm
folder1/folder2/folder3/item2.htm
folder1/folder2/folder2-fodler3/item3.htm



These 3 are the most common patterns so far (serving around 650 urls out of the 860).



Each folder name is actually a category/subcategory/keyword i can use to determine the properties of my item. As a general rule, the first 2 folders (folder1 and folder2) are the category and subcategory of the product, while the next folders (folder 3 and after that) determines the section (if any) of the item. I hope this makes sense.



So, one thought is to use the old, static, url as a database filed and match this to serve the content, something like:




RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /script.php?path=$1 [L]



I think that putting this rule at the very end of my rewrite rules will ensure that all other rewrites are unaffected.



Is this approach advisable? OR is there another way? Something more robust/secure/light?


Source: Tips4all

1 comment:

  1. The method you describe is pretty common and well:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule (.*) /script.php?path=$1 [L]


    You can lighten it up by parsing the original request string in your PHP script directly, which is more direct. Look what your server offers in the $_SERVER PHP superglobal array, e.g. run var_dump($_SERVER);. You already find something that is similar to your $_GET['path'] you're making use of, this is generally more inter-operable:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule (.*) /script.php [L]


    Next to that, depending on apache server version, it can be further on simplified as:

    FallbackResource /script.php


    This spares you the extra file-check in the RewriteCond you currently use.

    ReplyDelete