dongyingjiu0669 2017-06-03 15:56
浏览 49
已采纳

如何告诉mod_rewrite只在输入特定网址时重写?

I am having an issue with my .htaccess / .php files I set up my web project in a way that it's split into 3 levels:

  • base level (mywebsite.com)
  • pages (mywebsite.com/news)
  • subpages/articles (mywebsite.com/news/123)

And all three are working as expected, here's my .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?page=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?page=$1&sub=$2 [L]

I have to admit, I am not very good with the Regular Expressions for .htaccess/mod_rewrite as it is something I have done very rarely up to this point.

However my issue now is, that people dont get sent to a 404 error page if the page id (see $1) is not valid, like people can technically enter mywebsite.com/asdasdasd/ and it'll bring them (obviously) to the index.php, now my question is, can this be done some way in the .htaccess or should I just use php to check if the id they entered is valid or not? I did see that there's a thing called rewriteMap but I dont want to change the httpd.conf

Can anyone help?

  • 写回答

2条回答 默认 最新

  • dpfln86244 2017-06-03 17:06
    关注

    can this be done some way in the .htaccess or should I just use php to check if the id they entered is valid or not?

    I don't think this is programming problem but logical one.

    • Apache doesn't know what pages are valid /news /contact /help etc.
    • PHP knows what pages are valid

    So you have only two options

    a) Specify every single possible site in .htaccess which is terrible practice

    b) Check in PHP is page valid then throw 404 which is only one reasonable solution.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站