douhua1890 2013-06-20 09:44
浏览 75

使用.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.

  • 写回答

1条回答 默认 最新

  • dongsui4658 2013-06-20 09:47
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制