【问题标题】:How to stop people accessing the normal path but allowing it to be used in Mod-Rewrite如何阻止人们访问正常路径但允许它在 Mod-Rewrite 中使用
【发布时间】:2012-06-06 11:29:08
【问题描述】:

我正在使用 mod-rewrite 来制作真正的网址

www.example.com/public/questions.php?question_id= <int>

www.example.com/questions/ <int>

然后如何禁止以前的地址用作 REQUEST_URI 并只允许它们通过我的新缩短 url?

如果我提出一个规则

RewriteRule ^public public/page404_ready.php

这也使我当前更改的所有网址都没用了。


(如果我确实完成了这一切,我是否必须将页面中所有我自己的 (AJAX) 链接更改为新的 URL?)

这是我现在的文件

RewriteRule ^questions/topics/(.+)/?$ public/questions.php?topics=$1 [NC,L]
RewriteRule ^questions/ask/?$ public/ask_question.php [NC,L]
RewriteRule ^questions/edit/([0-9]+)/?$ public/edit_question.php?question_id=$1 [NC,L]
RewriteRule ^questions/posts/edit/([0-9]+)/?$ public/edit_post.php?post_id=$1 [NC,L]
RewriteRule ^users/edit/([0-9]+)/?$ public/edit_profile.php?user_id=$1 [NC,L]
RewriteRule ^(admin)/?$ public/$1/index.php [NC,L]
RewriteRule ^(admin)/(\w+)/?$ public/$1/$2.php [NC,L]
RewriteRule ^(admin)/(\w+)/(\d+)/?$ public/$1/$2.php?topic_id=$3 [NC,L]

RewriteRule ^([\w\-]+)/?$ public/$1.php [NC,L]

#RewriteRule ^public public/page404_ready.php [L]

【问题讨论】:

  • (是的,您也需要更改所有链接和ajax url)

标签: php .htaccess url mod-rewrite


【解决方案1】:

我不明白你到底想用这条规则做什么:

RewriteRule ^public public/page404_ready.php

您可以使用以下代码:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# this is your current rule
RewriteRule ^questions/([0-9]+)/?$ public/questions.php?question_id=$1 [L,NC,QSA]

# this is the URL you want to block
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+public/questions\.php\?question_id=[0-9]+[&\s] [NC]
RewriteRule ^ - [F]

【讨论】:

  • 我不明白,我现在用我的文件更新了我的问题,如何阻止人们访问 www.example.com/public/questions.php?question_id=
  • 上面的第二条规则就是这样做的,即阻止人们去www.example.com/public/questions.php?question_id= &lt;int&gt; 如果你把这条规则放在你的 .htaccess 中并访问那个 URI,你会得到 Forbidden 错误。
  • 在我的 PHP 代码中执行此操作会更好,即在所有那些只有在输入正确 URL 时才会重定向的页面上继续重定向回我自己的样式 URL?
  • [A-Z]{3,} 基本上匹配 GET、POST、PUT、DELETE 等 HTTP 方法。与其编写代码,不如通过 .htaccess 本身处理此问题,这在未来的更改周期中易于维护。
  • 哇,这真的很有帮助,谢谢。或许您也可以帮我理解为什么我的 example.com/public 页面有一个重定向循环?
猜你喜欢
  • 1970-01-01
  • 2016-03-06
  • 2012-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-26
  • 2012-07-28
  • 2019-04-22
相关资源
最近更新 更多