Forum Moderators: phranque
i.e.
RewriteRule ^/$ /default.php?id=52 [L]
RewriteRule ^/products$ /products.php?id=56 [L]
RewriteRule ^/products/a$ /detail.php?id=62 [L]
Is there a way I could use reg expression to just use one line of code to do the mapping?
The problem is that you nust 'tell' Apache that ""==52, "products"==56, and "products/a"==62. That's the only challenge here.
You can do that with a lookup table, or even access your current CMS/database with a small script to do a lookup by using mod_rewrite's RewriteMap if you have access to httpd.conf or other server-config files.
Jim
If you want to rewrite a URL to a filename, and the URL does not contain the 'text' of the filename in some easily-transcribable form, then yes, it is complicated -- It requires the use of a mapping function to associate the query string value "56" with "products", and the easiest way to do that is with a secondary (hand-coded) lookup table or with a script that can access the database where that association already exists.
Jim
I'd suggest backing off and doing some research.
I've had to answer in generalities here, because your specific application was not made clear. "SEF" plug-ins are available for some blog and forum software packages; If that's what you're working on, look into using them to make your URLs search engine friendly.
The Apache mod_rewrite documentation describes using RewriteMap to call a PERL script to look up URL-to-filename translations. If you have some programming experience, then there's no time like the present to study-up and implement this method before the number of URLs on your site gets out of hand. If you're not a programmer at all, then perhaps you could hire someone to write the code for you -- The code itself is not usually difficult, and the major work is adapting it your specific (blog, forum, etc.) software and its underlying database format.
If you're not using blog or forum software, and this is a custom-coded site, then consider whether the script that produces your pages could be modified to accept the friendly URLs and handle the translation internally.
Finally, if none of those solutions fit, consider changing your proposed friendly URL layout, and include the "ID number" in the friendly URL itself. It's not as pretty, but you still get the advantages of static-looking URLs vis-a-vis straightforward search engine indexing, and keyword-in-URL. This is a simple matter of using URLs like "/products/56/" and "/products/a-62/", for example. Here, the static-looking URL "carries" the ID numbers, and simple mod_rewrite code can be used to rewrite all such URLs with just a few lines of code -- Code it once and you're done.
Jim