Forum Moderators: phranque

Message Too Old, No Replies

My first mod rewrite to take uri path to database lookup

         

jmichaels

3:58 am on May 29, 2008 (gmt 0)

10+ Year Member



I have been reading as much as I can on this, and can not get the expected results.

at example.com/index.php I load a random image, that image will link to something like:
example.com/pictures/cars/1345

The path /pictures/cars/1345 does not exist, all I want to do is pull the 1345 away, and use that to lookup a record in a database. There never will be a url in the form of index.php?id=1345, as I am starting fresh with this site. I am able to successfully use php to grap the 1345 our of the uri.

I tend to be able to get this to work with a rewrite rule
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

The problems I am running into are with how the rest of the site needs to work. So if someone goes to example.com/dasda/dsad/21321 since that path does not exist, it should 404 on them. I only want this feature working for urls that are /pictures/cars/some_number

If that real path did in fact exist, it should load the page.

I have /faq/ as a real directory, and /faq/dasd/das/dadas is obviously not, though both of those seem to get intercepted by the rewrite rule, rather than getting a 404 served up.

I think that is it, much appreciated.

jdMorgan

4:26 am on May 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your code did exactly what you wrote: "If requested non-blank URL-path does not resolve to an existing file or directory, rewrite it to /index.php". Therefore, no URL-path requested from your site would ever result in a 404 except for "example.com/" which would result in a 404 (because the URL-path is blank) if it did not exist.

Simplify:


Options +FollowSymLinks
RewriteEngine on
#
RewriteRule ^pictures/cars/[0-9]+$ /index.php [L]

Please don't use mod_rewrite on your site without studying the documentation and understanding each and every line of your code completely. Otherwise, you may end up causing yourself a great deal of trouble -- more trouble than we can provide a "quick-fix" for here...

Be aware that in order for this to work, you must be using AcceptPathInfo. Therefore, this code (and your site) will only work on Apache 2.0 and later. Apache 1.3.x and earlier does not support the AcceptPathInfo directive. Apache 1.3 and earlier would have to use the "GET" method -- That is, change the script to use GET parameters, and change the rule to something like "RewriteRule ^pictures/cars/([0-9]+)$ /index.php?pic_num=$1 [L]"

Jim

jmichaels

2:20 am on May 30, 2008 (gmt 0)

10+ Year Member



@jd, I can not seem to figure out how to quote a reply...

I have read as much of the docs as I can grok, I have specific uses that seem pretty common, but I can not wrap my head around, so I came here.

Server version: Apache/1.3.41 (Darwin)

I am working as always, on a test site, which is more or less a staging ground where I will move these edits to when I test them to make sure they work.

I just put in your sample, and even though I am on 1.3, it seems to work. I enabled rewrite logging, and it appears to be doing correct.

http://example.com/pictures/cars/21312
in php, I am able to get back
Array ( [0] => [1] => pictures [2] => cars [3] => 21312 )

Any other messing with the URL gives me a 404.

I tried your 1.3x specific one, it works as well.

All I am doing is using php to take the current url and split it apart on '/' and give me the chunk I want. Thanks for your help, is there any reason you think I am still doing something wrong?

jdMorgan

3:29 pm on May 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



...[do] you think I am still doing something wrong?

No, not if you've thoroughly tested the code and it works. :) That is, after all, what really matters.

Jim