View Full Version : PHP: Files that don't really exist
rendezvouscp
Mar 18, 2006, 11:44 PM
Hello. I'm fairly new to PHP (I've been learning PHP thanks to Practical PHP Programming (http://hudzilla.org/phpbook/)), and I'm interested in learning about how to access files that don't really exist. For example, on my WordPress blog, the permalink structure doesn't really exist on the server, but you can still go to those urls and access files.
I know that WP, and presumably most everything out there, uses mod_rewrite in the .htaccess file. However, I can't seem to figure out what they're doing, or what it's called. If anyone could point me in the right direction, that'd be great.
Thanks in advance.
-Chasen
Coolnat2004
Mar 18, 2006, 11:52 PM
It's just the use of mod_rewrite, as you've said. The Apache manual is rather verbose about it, but there are some (http://www.phpfreaks.com/tutorials/23/0.php) helpful (http://www.higherpass.com/apache/Tutorials/Learning-To-Use-Mod_rewrite/) tutorials (http://www.widexl.com/scripts/documentation/mod_rewrite.html) out there.
Basically, it takes something like this:
http://www.pending.be/rss.php?tz=GMT-5&type=tag&key=birthday
and lets it appear to be this:
http://www.pending.be/rss/GMT-5/tag/birthday.rss
The .htaccess entry for that would look like this:
RewriteRule ^rss/(.*)/(.*)/(.*).rss http://www.pending.be/rss.php?tz=$1&type=$2&key=$3 [L,NC]
rendezvouscp
Mar 19, 2006, 03:58 PM
Thanks Coolnat2004 for the help, although I'm a little bit lost. I looked at those tutorials and tried it out, but can't seem to get it working.
I don't know if I'm placing the .htaccess in the wrong directory or if I'm writing the rewrite rules incorrectly. My goal is to have a url like "http://labs.chasenlehara.com/flickr/index.php?type=archive&page=6" map to "http://labs.chasenlehara.com/flickr/archive/page/6/" so that the URLs are cleaner. I'm trying to put this into the .htaccess file:
RewriteEngine On
RewriteBase /flickr/
RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/ /index.php?type=$1&year=$2&month=$3&day=$4&ispage=$5&page=$6&id=$7
All of the extra "holders" are for various other pages (basically I'm replicating Flickr's URLs on my own site).
Any ideas? Thanks for taking the time. :)
-Chasen
Coolnat2004
Mar 19, 2006, 10:43 PM
It looks like all of your holders are your problem. See, to match the pattern on the left, the URL must have all of the stated characters.
So, a valid URL for your match would be:
http://labs.chasenlehara.com/flickr/1/2/3/4/5/6/7/
if I am not mistaken. If /flickr/ is a directory, I don't think you need the RewriteBase as long as the .htaccess file is placed in the directory itself. Also, in the second part you might want to enter the full URL just to avoid confusion, because it looks like right now you're trying to send it to http://labs.chasenlehara.com/index.php?etc
In general.. this stuff is confusing, sometimes inconsistent, and frustrating. But, once you get it working it's smooth.
rendezvouscp
Mar 24, 2006, 08:29 PM
In general.. this stuff is confusing, sometimes inconsistent, and frustrating. But, once you get it working it's smooth.
No kidding. I'm unsure which site (http://ma.gnolia.com/tags/mod_rewrite) actually got me to where I am (a combination, with a stroke of good luck, really), but I finally figured it out a day or so ago. Thanks again for the help.
-Chasen
Coolnat2004
Mar 24, 2006, 09:27 PM
Alright cool, I'm glad you got it working. :D
vBulletin® v3.6.10, Copyright ©2000-2009, Jelsoft Enterprises Ltd.