为了账号安全,请及时绑定邮箱和手机立即绑定

.htacces 文件和 php 中的 get 方法有问题

.htacces 文件和 php 中的 get 方法有问题

PHP
肥皂起泡泡 2021-11-13 17:21:48
我有 company.php?name=Son's.&.clothes 的 URL当我使用.htaccess文件时,它应该像 company/Son's.&.clothes$url = $pro->vendor_company; ///$url = str_replace('&', '%26', $url);// Here i replaced & with %26http://localhost/worldsindia/name/Jai%20Industries%20Ltd. // Then it looks like that输出是我在 PHP 中使用 GET 方法从 URL 获取名称: if(isset($_GET['name'])){        $name = $_GET['name'];        }    var_dump($name);   Jai Industries Ltd.php 这是我的.htaccess文件RewriteEngine On<ifModule mod_headers.c>Header set Connection keep-alive </ifModule>## EXPIRES CACHING ##<IfModule mod_expires.c>ExpiresActive OnExpiresByType image/jpg "access 1 year"ExpiresByType image/jpeg "access 1 year"ExpiresByType image/gif "access 1 year"ExpiresByType image/png "access 1 year"ExpiresByType text/css "access 1 month"ExpiresByType text/html "access 1 month"ExpiresByType application/pdf "access 1 month"ExpiresByType text/x-javascript "access 1 month"ExpiresByType application/x-shockwave-flash "access 1 month"ExpiresByType image/x-icon "access 1 year"ExpiresDefault "access 1 month"</IfModule>## EXPIRES CACHING ###AddOutputFilterByType DEFLATE text/plain#AddOutputFilterByType DEFLATE text/html#AddOutputFilterByType DEFLATE text/xml#AddOutputFilterByType DEFLATE text/css#AddOutputFilterByType DEFLATE application/xml#AddOutputFilterByType DEFLATE application/xhtml+xml#AddOutputFilterByType DEFLATE application/rss+xml#AddOutputFilterByType DEFLATE application/javascript#AddOutputFilterByType DEFLATE application/x-javascriptRewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^([^\.]+)/?$ $0.php [NC,L]RewriteRule ^company/([A-Za-z0-9-\s&()+/.']+)  company.php?name=$1 [NE,B,L]$name 的输出就像 Son's
查看完整描述

1 回答

?
小唯快跑啊

TA贡献1863条经验 获得超2个赞

请求的 url 不是 urlencoded。


& 字符表示获取请求中的新参数。


url [questionmark] parametername [equals sign] value [ampersand] parametername [equals sign] value


url?foo=bar&baz=bal

会变成:


$_GET = [

    'foo' => 'bar',

    'baz' => 'bal'

];

所以同样的逻辑适用于你的 url company.php?name=Son's.&.clothes,然后在你的情况下你得到


$_GET = [

    'name' => 'Son\'s.',

    '.clothes' => null,

];

或者如果您的 webserver/php 设置不同,空的 get 变量可能会被删除。


urlencode()在使用 php 或 javascript 渲染它们之前使用encodeURIComponent()或通过手动编码/替换&符号%26


company.php?name=Son's.%26.clothes

基于编辑的问题


$url = str_replace('&', '%26', $url);// Here i replaced & with %26

$url = urlencode($url);

删除第一行。urlencode 会处理这个问题。现在您正在对 %26 的转义符号进行 urlencoding。这两行应该变成:


 $url = urlencode($url);

哪个会给


 http://localhost/worldsindia/name/Durgesh+Jaiswal+%26+Co

现在你的重写规则是荒谬的


 RewriteRule ^company/([A-Za-z0-9-\s()+/']+)  company.php?name=$1 [NC,L]

这将检查 url 是否以公司一词开头。

您的 url 以 worldsinda 开头

即使它通过将 company 换成 worldsindia 来匹配,那么您的 url 也会变成:


 company.php?name=name/Durgesh Jaiswal & Co

这是一个奇怪的获取请求。但是因为它是重写规则而不是重定向规则,它再次从顶部输入到 .htaccessfile 中。


所以你现在拥有的是网址:


 company.php?name=name/Durgesh Jaiswal & Co

这会将 & 解释为参数分隔符,它将删除第二个参数,因为它是空的。所以你最终得到


array(1) {

  ["name"]=>

  string(21) "name/Durgesh Jaiswal "

}

现在要修复它,您需要修复您的重写规则


RewriteRule ^company/([A-Za-z0-9-\s()+']+)/([A-Za-z0-9-\s()+']+)  company.php?company=$1&name=$2 [NC,L]

在这里,我们设置了基本 url,/company因此 company 之后的任何内容都将被提供给 company.php 文件。任何不是公司的东西都会被喂给别的东西。


现在他得到请求的参数是


array(2) {

  ["company"]=>

  string(11) "worldsindia"

  ["name"]=>

  string(16) "Durgesh Jaiswal "

}

我们仍然没有 & 号之后的部分,这是因为 Apache 做了一些事情,但主要是因为您的重定向规则中没有 & 号。


RewriteRule ^company/([A-Za-z0-9-\s()&+']+)/([A-Za-z0-9-\s()+&']+)  test.php?company=$1&name=$2 [NE,L]

现在我们结束了:


array(3) {

  ["company"]=>

  string(13) "worldwideinda"

  ["name"]=>

  string(16) "Durgesh Jaiswal "

  ["Co"]=>

  string(0) ""

}

我们可以通过在重写规则中添加 B 修改器来解决这个问题,阅读更多信息https://www.gerd-riesselmann.net/development/using-and-path-apache-modrewrite-and-php/


 RewriteRule ^company/([A-Za-z0-9-\s()&+']+)/([A-Za-z0-9-\s()+&']+)  test.php?company=$1&name=$2 [NE,B,L] 

这将导致


array(2) {

  ["company"]=>

  string(13) "worldwideinda"

  ["name"]=>

  string(20) "Durgesh+Jaiswal+&+Co"

}

由于优点,这不是很好看。最好让 url 编码用正确的转义码 %20 替换它们。你可以用rawurlencode做到这一点

//img1.sycdn.imooc.com//618f83d00001c27e06900218.jpg

查看完整回答
反对 回复 2021-11-13
  • 1 回答
  • 0 关注
  • 188 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号