提问人:ganjo ali 提问时间:11/10/2023 最后编辑:ganjo ali 更新时间:11/10/2023 访问量:113
拒绝对正在执行的脚本的直接访问
Deny direct access to executing script
问:
我以前在下面工作正常,
RewriteEngine on
RewriteRule ^([0-9]+)/?$ /news/index.php?post=$1 [L,QSA]
但我不希望它像这样打开
https://example.com/news/index.php?post=123456
我希望它只以只允许数字的方式打开
https://example.com/news/123456
是 /news/index.php?post=123456 不应解析的请求 只有 /news/123456 应该可以访问
谢谢
答:
-1赞
Prakash B
11/10/2023
#1
嗨,要实现这一点,请添加以下行并重新启动服务,其中 提供的规则确保 URL 以“news/”开头,并捕获数值以根据您的期望将它们附加到 URL。
RewriteEngine on
RewriteRule ^news/(\d+)$ /news/index.php?post=$1 [L,QSA]
评论
0赞
ganjo ali
11/10/2023
谢谢你的回复 但我想防止它像这样打开 example.com/news/index.php?post=123456 如果有人这样做,请直接将他们重定向到此链接 example.com/news/123456
1赞
arkascha
11/10/2023
应该重新启动什么服务?如果这样的规则是在分布式配置文件中实现的,如问题的标签所建议的那样,则无需重新启动任何内容。
0赞
arkascha
11/10/2023
#2
您需要将两者结合起来,即内部重写和外部重定向:
RewriteEngine on
# externally redirect direct requests to the actual resource
RewriteConf %{QUERY_STRING} ^post=(\d+)$
RewriteRule ^/?news/index\.php$ /news/%1 [R=301,L]
# internally rewrite requests to pretty URLs
RewriteRule ^/?news/(\d+)$ /news/index.php?post=$1 [END]
最好从临时重定向开始,只有在一切按预期工作时才将其更改为永久重定向。这样可以防止在您仍在尝试时出现客户端缓存问题。R=302
R=301
上述规则在中央 http 服务器的配置和分布式配置文件 (“.htaccess”) 中同样有效。如果您有权访问中央配置,那么这显然应该是实现此类规则的首选位置。
评论
RewriteRule ^news/([0-9]+)/?$ /news/index.php?post=$1 [L,QSA]
/news/index.php?post=123456
/news/123456