Forum Moderators: phranque
What I want to achieve is to create virtual paths such as www.domain.com/products/ or www.domain.com/products/erv/ with trailing slashes that is passed into mod_rewrite to process this virtual path. Which breaks up the path into separate values (i.e. “products”, “erv”). Then queries the database for the topic id to determine if “erv” and/or “products” exists. If it does exist the topic id would be returned to the page to be processed other wise a 404 error should occur to the client. I don’t want to have to input direct references to all the paths into Apache’s httpd.conf or .htaccess as I have 11 topics with 70+ sub-topics to deal with. Going to a database is the best option I believe. In theory this should work however I’ve tried my own mod_rewrite RewriteRules and RewriteMap with no success of a value being returned. I’ve even tried examples that demonstration a similar method but no success. If anyone has an idea how to achieve this or can direct me into the right direction please let me know.
PHP code example to break up the path into separate values.
list($value1, $value2, $value3) = explode(”/”, $_SERVER[’SCRIPT_URL’]);
httpd.conf mod_rewrite in VirtualHost directive.
RewriteMap content prg:/var/www/htdocs/venmar/getTopicId.php
RewriteRule ^(.*)/(.*)/$ /index.php?nTopicID=${content:$1¦0}
From the looks of your RewriteRule pattern, I surmise that you are testing this code in .htaccess. Be aware that RewriteMap is not available in .htaccess, only in httpd.conf. So this may be a cause of trouble.
The simplest way to do this is probably to rewrite only "page" requests --not images, CSS stylesheets, external JS files, or robots.txt-- to your script. Then have the script verify that the requested content exists. If not, the script can output the 404 response header. This method simplifies the design and lets your database do all the work, for which it is best-suited.
Something like:
# If requested URI does not exist as a directory
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite request to script
RewriteRule ^(.+)/$ /index.php?nTopicID=$1 [L]
Make sure your script does not write any output to STD_OUT before deciding if the content exists. Otherwise, you will get the dreaded "headers already written" error message. You should only begin output once you know whether to return a 404-Not Found or a 200-OK response.
Jim
Yes this is correct, however I still wish to achieve content request by a user going to /products/erv/, rather than /index.php?nTopicID=1&nValue=2&nValu3=1 using RewriteMap prg.
This is a great example however this is not what I'm wishing to achieve. I wish to do this using RewriteMap with prg as I don't want to manually have to enter 11+ topics and 70+ subtopics.
Yes this is what I'm concerned about and want to make sure this functions correctly.
However, if you want to use RewriteMap, then be aware that the pattern in .httpd.conf, unlike that in .htaccess, should start with a slash:
RewriteRule [b]^/([/b][^.]+)/([^/]+)/$ /index.php?nTopicID=${content:$1¦0}
Jim
Oh I was not aware the / was required when doing this in the httpd.conf, thanks. Mmm well let me try this step and see if this works. But then again I don't want to do this as you say I would require two databases. Oh man I'm confused as to what to do to achieve my goals. Please tell me if I'm not making sense as to what I'm trying to do here (smile).
As long as the URL carries the information --regardless of form-- needed to determine that the request needs to be passed to the script, plus the information needed by the script to determine what content to serve, then this is both possible and easy, The only need for mod_rewrite is to pass the requests to the script, and to move the information into a query string if this is required to keep the script simple.
It's quite possible that you are imagining this to be much more complicated than it is. But it's basically just identifying "special" requested URLs, passing those requests to the script, and giving the script the info it needs to decide what content to serve. It all boils down to passing the necessary information from a link on one of your pages clicked by a user, from the browser's HTTP request to the script.
Rather than starting this thread with a specific implementation, it might have been better to to start with a "How do I?" - type query with some example URLs.
Jim
Thanks for the help.