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条)

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: