1

关于系统

我的项目中有这种格式的 URL:-

http://project_name/browse_by_exam/type/tutor_search/keyword/class/new_search/1/search_exam/0/search_subject/0

其中keyword/classpair表示使用“class”关键字进行搜索。

以下是我的 htaccess 文件:-

##AddHandler application/x-httpd-php5 .php

Options Includes +ExecCGI
Options +FollowSymLinks

<IfModule mod_rewrite.c>
RewriteEngine on

############To remove index.php from URL

RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
#################################################end of find a class 


</IfModule>

我有一个通用的 index.php 文件,它为项目中的每个模块执行。只有一个重写规则可以从 URL 中删除 index.php(如上所示)。

我没有使用任何 htaccess 重写规则来定义 $_GET 数组。我在 PHP 中有一个 URL 解析器函数来代替它。对于我给出的示例 URL,解析器返回:-

Array ( [a] => browse_by_exam [type] => tutor_search [keyword] => class [new_search] => 1 [search_exam] => 0 [search_subject] => 0 )

我在准备搜索 URL 时使用 urlencode(),在阅读搜索 URL 时使用 urldecode()

问题

我遇到了 URL 中某些字符的问题

Character               Response
%                       400 - Bad Request - Your browser sent a request that this server could not understand.
/                       404 - Not FOund
\ # +                   Page does not break but urldecode() removes these characters.

我想允许所有这些字符。可能是什么问题呢?我如何允许这些?请帮助谢谢, Sandeepan

更新

现在只有 / 字符会导致 URL 中断(像以前一样的 404 错误)。因此,我尝试删除隐藏在 URL 中的 index.php 的 htaccess 重写规则,并尝试使用完整的 URL。class/new对于我尝试使用以下两个 URL的搜索词:-

http://project_name/index.php?browse_by_exam/type/tutor_search/keyword/class%2Fnew/new_search/1/search_exam/0/search_subject/0

http://project_name/index.php/browse_by_exam/type/tutor_search/keyword/class%2Fnew/new_search/1/search_exam/0/search_subject/0

第一个有效,但第二个无效。注意index.php?browse_by_exam第一个。

但我不能使用第一个 URL 约定。我必须隐藏 / 使用 index.php。请帮忙

再次感谢桑迪潘

编辑(已解决)

考虑到 Bobince 对我的另一个问题的回答

urlencoded 正斜杠破坏了 URL ,我觉得最好有这样的 URL:- http://project_name/browse_by_exam?type/tutor_search/keyword/class %2Fnew/new_search/1/search_exam/0/search_subject/0

这样我就摆脱了由&param1=value1&param2=value2约定引起的可读性困难,并且还能够通过使用允许在查询字符串部分使用正斜杠?

我想避免 AllowEncodedSlashes 因为 Bobince 说Also some tools or spiders might get confused by it. Although %2F to mean / in a path part is correct as per the standard, most of the web avoids it.

4

1 回答 1

2

有些问题听起来像是与您尝试使用有关PATH_INFO(您将RewriteRule所有东西都放在后面index.php,好像它是一条路径)。是否可以只使用该$_SERVER['REQUEST_URI']变量作为 URL 解析器函数的输入?它包含相同的信息,我觉得它的问题会更小。

尝试创建解决方案在 per-dir ( ) 上下文PATH_INFO中似乎效果不佳。.htaccess您可以设置AllowPathInfo On,但是一旦mod_rewrite尝试在内部重定向 URL,Apache 似乎不想解析 URL 的尾随部分,这会导致 404 错误。

如果你$_SERVER['REQUEST_URI']改用,那么你可以只重写index.php没有尾随信息,如下所示:

RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]

就 400 错误而言,您%应该被编码为%25by urlencode(),但无论出于何种原因,这听起来都可能存在问题。我会检查以确保您的搜索 URL 确实在发送到浏览器的输出中正确编码,因为这也可能与其他剩余字符的问题有关(但我不确定)。

编辑:如果你使用上面的重写,你会有像这样的 URL

http://project_name/browse_by_exam/type/tutor_search/keyword/class/new_search/1/search_exam/0/search_subject/0

他们将在内部重定向到index.php. 然后,你可以得到零件

/browse_by_exam/type/tutor_search/keyword/class/new_search/1/search_exam/0/search_subject/0

$_SERVER['REQUEST_URI']该脚本中(它将包含此值),然后您可以像现在一样解析它。我不确定为什么您必须能够在 之后对其进行重写index.php,因为即使不是,您也可以获得此信息,并且对于浏览器中的用户来说,它看起来完全一样。$_SERVER['PATH_INFO']如果使用的部分不可更改,您甚至可以在脚本开头执行此操作:

$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'];

如果你真的不能这样做,我不确定是否有解决方案(在你的另一个问题中解释了为什么这是有问题的),但我会看看它是否有可能并且尽快给您回复。

于 2010-07-12T13:31:18.770 回答