【问题标题】:Apache - combo newbie question on mod rewrite & restrict file access by URLApache - 关于 mod 重写和通过 URL 限制文件访问的组合新手问题
【发布时间】:2011-06-01 10:29:41
【问题描述】:

这是我第一次尝试重写 URL 和文件访问限制规则。我已经阅读了堆栈和谷歌中的相关帖子,但到目前为止未能获得完整的答案。到目前为止,网络上的所有研究信息似乎都是零碎的,总是缺少一些关键步骤。

像我这样的新手发现很难将这些点联系起来,所以我求助于stackoverflow 的大师寻求帮助。

我将列出到目前为止我已采取的所有步骤,如果有人可以指导我做错了什么,我将永远感激不尽。也许我的一步一步的描述将有助于其他人在未来寻找相同的答案。

Apache 配置

1) 在httpd.conf 文件@conf 文件夹中取消注释LoadModule rewrite_module modules/mod_rewrite.so 行。

2) 通过使用 phpinfo() 函数显示 php 摘要详细信息并搜索字符串“mod_rewrite”来检查 mod_rewrite 是否已加载。

我想要实现的目标

[网址重写]

**from** : www.domain.com/listing.php?
           category=men+casual+pant&title=blue+office+pant&item_ID=123

**to** : www.domain.com/category/men/casual/pant/blue-office-pant_123.html

[限制以下文件的直接访问]

**restrict file** /htdocs/bg_addEditItem.php from direct URL access typing

到目前为止我做了什么,但没有任何效果

1) 创建一个 .htaccess 文件并将其放在 /htdocs 文件夹中,内容如下

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^category/$1/$2/$3/$4-$5-$6_$7 listing.php?&category=([a-z]+)-([a-z]+)-([a-z]+)&title=([a-z]+)+([a-z]+)+([a-z]+)item_ID=([0-9]+)

2) 目前我使用 session_id 和 HTTP-referrer 的组合来拒绝直接访问 bg_addEditItem.php 文件,但我认为必须有一个更简单的方法,将所有受保护的文件放在一个文件夹中并设置不同的 @987654326 @ 文件。有人可以展示这是如何完成的吗?我访问此文件的方法来自表单的帖子。

非常感谢。

【问题讨论】:

    标签: apache .htaccess mod-rewrite


    【解决方案1】:
    RewriteCond %{QUERY_STRING} ^category=(.*)+(.*)+(.*)&title=(.*)+(.*)+(.*)&itemID=(.*)$
    RewriteRule ^$ ^category/%1/%2/%3/%4-%5-%6_%7.html [L]
    

    第二部分

    RewriteCond %{HTTP_REFERRER} !^http://(www\.)?yourdomain.com/(.*)$
    RewriteRule ^/protectedfolder/(.*) - [R=404]  [L]/* Apache will check if url points to your protected folder and if yes , it will check above condition . Above condition will see if the referrer is from you site , if not it will pass and 404 will be returned to user.
    

    【讨论】:

    • 嗨@Rizwan Sharif,我会试试看。关于通过 URL 限制直接文件访问的第二部分怎么样。你知道这是怎么做到的吗?
    • 对不起,我没有很好地理解您的第二部分,您是否使用重写规则来实现 SessionID + HTTP_REFERRER 解决方案?
    • 请看我已经改变了我的答案,我打错了一些东西
    • @Rizwan Sharif - 我的意思是,我想保护bg_addEditItem.php 以防止用户通过 URL 直接访问文件。我想知道是否可以应用 .htaccess 技术来保护文件夹,以便用户无法直接访问它,而只能通过我网站中的链接访问它。
    • @Rizwan Sharif,太棒了,这正是我所需要的!
    猜你喜欢
    • 1970-01-01
    • 2011-03-03
    • 2011-04-19
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 2011-01-21
    • 2011-03-26
    相关资源
    最近更新 更多