Forum Moderators: phranque

Message Too Old, No Replies

How to mod rewrite with regular expression?

         

wll6568

7:24 am on Jul 30, 2008 (gmt 0)

10+ Year Member



Apparently, I have to hardcode each page to map to the right page with paremeters.

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?

jdMorgan

1:25 pm on Jul 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you have just a few URLs to map, then one-rule-per URL is the most-efficient approach.

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

wll6568

10:40 pm on Jul 30, 2008 (gmt 0)

10+ Year Member



Um...now that sounds complicated to me. Um...Does anyone know how Wordpress do it? I saw from their .htaccess that they have just few lines of code and that could do the mapping.

jdMorgan

11:27 pm on Jul 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



They map all requests for URLs which do not resolve to existing 'real' files or directories to the WP script, and then use the back-end CMS (database) to look up the 'post' that is associated with the requested URL.

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

wll6568

6:13 am on Jul 31, 2008 (gmt 0)

10+ Year Member



Okay....that is too complicated for me, don't even understand what you were saying. But thanks anyway.

Just that I dont know if its okay to have a big .htaccess file especially when the number of pages mapped gets more and more.

jdMorgan

1:36 pm on Jul 31, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I certainly would not plan to use a method that results in an initially-large and constantly-growing .htaccess file full of rewrites.

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