【问题标题】:Having issue with .htacces file and get method in php.htaccess 文件和 php 中的 get 方法有问题
【发布时间】:2019-12-12 23:03:29
【问题描述】:

我有带有 company.php?name=Son's.&.clothes 的 URL

当我使用.htaccess 文件时,它应该是这样的 公司/儿子的.&.衣服

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

http://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 On
ExpiresByType 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-javascript

RewriteCond %{REQUEST_FILENAME} !-f


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

$name 的输出类似于Son's

【问题讨论】:

标签: php


【解决方案1】:

请求的 url 没有 urlencoded。

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

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.&amp;.clothes,那么在你的情况下你会得到

$_GET = [
    'name' => 'Son\'s.',
    '.clothes' => null,
];

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

urlencode() 使用 php 或使用 encodeURIComponent() 的 javascript 或手动编码/用 %26 替换与符号之前的 URL

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]

这会检查网址是否以单词 company 开头。
您的网址以 worldsinda
开头 即使通过将 company 换成 worldsindia 与之匹配,您的 url 也会变成:

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

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

所以你现在拥有的是 url:

 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 执行此操作

【讨论】:

  • 好的,我使用了 urlencode() 并且 url 像这样 Durgesh+Jaiswal+%26amp%3B+Co 但 get['name'] 仅是 Durgesh Jaiswal 以及如何替换 %26amp
  • @ShadowShoot 你正在发送"Durgesh+Jaiswal+&amp;amp;+Co" 那里有一个 html 实体,它可能由于某种原因被过于热心的防火墙阻止了。你可以试试"Durgesh+Jaiswal+%26+Co"
  • @ShadowShoot 您不需要,您的网络服务器会将其转换为正确的字符。据我所见imgur.com/6AvLwtI
  • 但我只得到 Durgesh Jaiswal 没有剩余
  • 你正在做的整个请求是什么?您是在执行 company.php 请求还是在执行 /company/whateverever 请求?你能粘贴你正在使用的网址吗?
猜你喜欢
  • 1970-01-01
  • 2019-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-29
相关资源
最近更新 更多