使用.htaccess更改网址会丢失内容

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:

  • You probably (and rightfully so) do not have register_globals enabled. So $page should actually be $_GET['page'].
  • Your query is prone to attacks, you should always mysql_real_escape_string() parameters going into your queries.
  • A user could enter non-numeric values. Those would not be catched but would probably end up in syntax errors.

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.