关于系统
我的项目中有这种格式的网址: -
http://project_name/browse_by_exam/type/tutor_search/keyword/class/new_search/1/search_exam/0/search_subject/0
其中keyword/class
对意味着使用“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 )
我在阅读搜索网址时准备搜索网址和urldecode()时使用urlencode()
问题
我在网址
中遇到一些问题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错误,如前所述)。所以,我尝试删除htaccess重写规则,该规则隐藏了URL中的index.php并尝试使用完整的URL。对于搜索字词class/new
,我尝试使用以下两个网址: -
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。请帮忙
再次感谢 Sandeepan
修改(已解决)
考虑到Bobince对我的另一个问题的回答
urlencoded Forward slash is breaking URL
,我觉得最好有这样的网址: -
http://project_name/browse_by_exam?type/tutor_search/keyword/class
%2Fnew/new_search/1/search_exam/0/search_subject/0
这样我就摆脱了¶m1=value1¶m2=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.
答案 0 :(得分: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错误而言,%
应由%25
编码为urlencode()
,但听起来无论出于何种原因都可能存在问题。我会检查以确保您的搜索网址确实在发送到浏览器的输出中正确编码,因为这可能与其他剩余字符的问题有关(但我不确定)。
修改:如果你使用了上面的重新创建,你就会有像
这样的网址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'];
如果你真的不能这样做,我不确定是否有解决方案(在你的另一个问题中有一个解释为什么这是有问题的),但我会看看它是否在一切皆有可能,并回复你。