Reputation: 58
I did some searching around, but I'm not very experienced in PHP, so I don't know the correct Terms/Words I am looking for. However, I do have a good idea of what I want to do.
Title
, Category
, Tags
, etc...)pinkflowers.php
, redroses.php
, etc...)So if someone visits "mysite.com/pinkflowers.php", the page should be output with the variables of pinkflowers
from my database.
However, the file pinkflowers.php
doesn't actually exist, only template.php
exists, which would be the "blueprint" for all the images in my database.
I would like the .php
to be removed from the URL in the browser.
"mysite.com/pinkflowers.php" => "mysite.com/pinkflowers"
I already have code that does this with my existing pages (below); I'm not sure if it will also work with these "imaginary" pages.
(.htaccess)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
If the user tries to visit a page that has no related database entry (eg: "mysite.com/430jfif0ij"), they should be re-directed to the homepage.
I'm not expecting anyone to give me ALL the answers, but please at least guide me in the right direction to begin making such a template. Thanks!
Upvotes: 0
Views: 337
Reputation: 58
I was able to solve my own question eventually, in the mean time that no Answers were given.
I followed this Tutorial here: http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/
My .htaccess
contained:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^category1/(.*)$ ./template.php?image=$1
RewriteRule ^category2/(.*)$ ./template.php?image=$1
RewriteRule ^category3/(.*)$ ./template.php?image=$1
etc...
category#
- being the "folders" that my images are categorized by.
I did this on the template.php
to extract the Variable from the URL:
$result = mysql_query('SELECT * FROM image_list WHERE image_name="' . mysql_real_escape_string($_GET['image']) . '"');
To redirect
the visitor to the Homepage if they enter an invalid URL/ID, I used this:
if (mysql_num_rows($result)==0) {
echo "<script>window.location = 'http://mysite.com/'</script>";
}
So the combination of all these methods achieved exactly what I was looking for.
Upvotes: 2
Reputation: 174987
Let your .htaccess be something like:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ template.php/$1
Then (on template.php
) use $_SERVER["path_info"]
to determine the path the user has entered (i.e. /pinkflowers
) and query the database accordingly.
The reason I like $_SERVER["path_info"]
is because it doesn't use (or pollute) the GET variable space.
Upvotes: 0