Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用htaccess和mod_rewrite忽略/接受正向斜杠_Php_.htaccess_Mod Rewrite_Redirect - Fatal编程技术网

Php 使用htaccess和mod_rewrite忽略/接受正向斜杠

Php 使用htaccess和mod_rewrite忽略/接受正向斜杠,php,.htaccess,mod-rewrite,redirect,Php,.htaccess,Mod Rewrite,Redirect,在这件事上我需要一些帮助 我正在制作一个php页面,该页面收集一个查询字符串,根据数据库对其进行匹配测试,然后将用户重定向到站点的不同部分 以下是它当前的工作方式(无需htaccess/mod_重写): 用户访问:domain.com/redirect/index.php?slug=Test_1 php页面清理并在数据库中查找“Test_1”,并检索目标URL以将用户重定向到。(例如domain.com/New_Test_1) 然后,php页面301会相应地重定向 这部分工作正常。但是,由于一些

在这件事上我需要一些帮助

我正在制作一个php页面,该页面收集一个查询字符串,根据数据库对其进行匹配测试,然后将用户重定向到站点的不同部分

以下是它当前的工作方式(无需htaccess/mod_重写):

  • 用户访问:domain.com/redirect/index.php?slug=Test_1
  • php页面清理并在数据库中查找“Test_1”,并检索目标URL以将用户重定向到。(例如domain.com/New_Test_1)
  • 然后,php页面301会相应地重定向
  • 这部分工作正常。但是,由于一些我无法控制的变量,我需要解释原始URL(使用htaccess/mod_rewrite),如下所示:

    • domain.com/redirect/index.php/Test_1
    其作用仍然与:

    • domain.com/redirect/index.php?slug=Test_1
    (注意:是的,index.php需要保留在url中。)

    在我的htaccess中,我有以下功能,但我知道它可能会更好:

    RewriteEngine On
    Options +FollowSymLinks
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/(.*)$ %{DOCUMENT_ROOT}/redirect/index.php?slug=$2 [PT,L,QSA]
    
    我需要帮助的部分…

    一些旧的url段塞中有向前斜杠,如下所示:

    • domain.com/redirect/index.php?slug=How\u to\u代码/程序
    如果没有htaccess,上述方法仍然有效,但在使用pretty(ier)url时失败:

    • domain.com/redirect/index.php/How_to_Code/Program
    在我当前的htaccess中,它只捕获“How_to_Code”部分,但忽略了后面的所有内容

    所以我的问题是:如何重新构造htaccess,以获取domain.com/redirect/index.php/(.*)之后的所有内容,包括前斜杠

    编辑:如果执行此操作,则.htaccess将进入/redirect目录

    RewriteRule ^redirect/index.php/([a-zA-Z0-9/_]+)$ redirect/index.php?slug=$1
    

    这将接受斜杠和下划线。要添加更多允许的字符,只需将它们添加到[]方括号内即可。某些字符可能需要转义,所以只需通过谷歌搜索即可。

    谢谢David,这个.htaccess将进入/redirect目录。我尝试了你的规则,但是捕获($1)没有返回任何结果。有什么想法吗?如果它在重定向目录中,那么只需将
    RewriteRule^index.php/([a-zA-Z0-9/]+)$index.php?slug=$1
    重定向到重定向文件夹中的index.php文件,同时保持像www.yoursite.com/redirect/index.php/Some\u values这样的url,我就可以准确地找到我需要去的地方了。我最终使用了
    RewriteRule^index.php/(.+)$index.php?slug=$1[PT,L,QSA]
    ,只是因为有很多字符的可能性(除了正斜杠)。这样做是否存在安全问题?当您允许更多字符时,总是存在更多的安全问题。最好知道需要哪些字符并坚持使用这些字符,但是如果字符的范围非常大,那么在执行databae查询之前,只需使用PHP addslashes()函数或mysql_real_escape_string()函数保护变量$_GET['slug'],或者更好地使用准备好的语句使其更安全。另外,如果您知道$slug值永远不会超过50个字符,那么在数据库查询之前检查其长度。这将防止黑客使用自己的长时间恶意sql查询