I am making a simple CMS, so the page links are domain/index.php?page=1
and so on (page2, page3...), and I am pulling the content out of the database with the following line:
$q = "SELECT name, content FROM pages WHERE page_id=$page";
That all works, but I wanted to change the look of the URLs, so I did with the following in .htaccess
RewriteRule ^([A-Za-z]+)/?$ index.php?page=$1 [NC,L]
The problem is, now when I click on the page I changed the url (in this case it was ?page=2), I don't pull the content out of the db, I guess because the MySQL query cant find $page
How do I remedy this?
EDIT:
I updated the rule to include numeric characters as well but it is still the same.
RewriteRule ^([A-Za-z0-9]+)/?$ index.php?page=$1 [NC,L]
EDIT2:
Just to hammer the point home, if I for example hardcode page=2
in the rewrite rule it works, but obviously I want that to happen dynamically. In this case $1
should become 2
.
Are you sure
$q = "SELECT name, content FROM pages WHERE page_id=$page";
is directly taken from your code? It has a few quite severe problems:
register_globals
enabled. So $page
should actually be $_GET['page']
.mysql_real_escape_string()
parameters going into your queries.My advice? There is no point in writing all this stuff yourself. Instead use an existing modern CMS or framework. 99% of the mistakes and security holes you'd end up creating have been taken into account in them already.