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

我有 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 %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


肥皂起泡泡
浏览 189回答 1
1回答

小唯快跑啊

请求的 url 不是 urlencoded。& 字符表示获取请求中的新参数。url [questionmark] parametername [equals sign] value [ampersand] parametername [equals sign] valueurl?foo=bar&baz=bal会变成:$_GET = [&nbsp; &nbsp; 'foo' => 'bar',&nbsp; &nbsp; 'baz' => 'bal'];所以同样的逻辑适用于你的 url company.php?name=Son's.&.clothes,然后在你的情况下你得到$_GET = [&nbsp; &nbsp; 'name' => 'Son\'s.',&nbsp; &nbsp; '.clothes' => null,];或者如果您的 webserver/php 设置不同,空的 get 变量可能会被删除。urlencode()在使用 php 或 javascript 渲染它们之前使用encodeURIComponent()或通过手动编码/替换&符号%26company.php?name=Son's.%26.clothes基于编辑的问题$url = str_replace('&', '%26', $url);// Here i replaced & with %26$url = urlencode($url);删除第一行。urlencode 会处理这个问题。现在您正在对 %26 的转义符号进行 urlencoding。这两行应该变成:&nbsp;$url = urlencode($url);哪个会给&nbsp;http://localhost/worldsindia/name/Durgesh+Jaiswal+%26+Co现在你的重写规则是荒谬的&nbsp;RewriteRule ^company/([A-Za-z0-9-\s()+/']+)&nbsp; company.php?name=$1 [NC,L]这将检查 url 是否以公司一词开头。您的 url 以 worldsinda 开头即使它通过将 company 换成 worldsindia 来匹配,那么您的 url 也会变成:&nbsp;company.php?name=name/Durgesh Jaiswal & Co这是一个奇怪的获取请求。但是因为它是重写规则而不是重定向规则,它再次从顶部输入到 .htaccessfile 中。所以你现在拥有的是网址:&nbsp;company.php?name=name/Durgesh Jaiswal & Co这会将 & 解释为参数分隔符,它将删除第二个参数,因为它是空的。所以你最终得到array(1) {&nbsp; ["name"]=>&nbsp; string(21) "name/Durgesh Jaiswal "}现在要修复它,您需要修复您的重写规则RewriteRule ^company/([A-Za-z0-9-\s()+']+)/([A-Za-z0-9-\s()+']+)&nbsp; company.php?company=$1&name=$2 [NC,L]在这里,我们设置了基本 url,/company因此 company 之后的任何内容都将被提供给 company.php 文件。任何不是公司的东西都会被喂给别的东西。现在他得到请求的参数是array(2) {&nbsp; ["company"]=>&nbsp; string(11) "worldsindia"&nbsp; ["name"]=>&nbsp; string(16) "Durgesh Jaiswal "}我们仍然没有 & 号之后的部分,这是因为 Apache 做了一些事情,但主要是因为您的重定向规则中没有 & 号。RewriteRule ^company/([A-Za-z0-9-\s()&+']+)/([A-Za-z0-9-\s()+&']+)&nbsp; test.php?company=$1&name=$2 [NE,L]现在我们结束了:array(3) {&nbsp; ["company"]=>&nbsp; string(13) "worldwideinda"&nbsp; ["name"]=>&nbsp; string(16) "Durgesh Jaiswal "&nbsp; ["Co"]=>&nbsp; string(0) ""}我们可以通过在重写规则中添加 B 修改器来解决这个问题,阅读更多信息https://www.gerd-riesselmann.net/development/using-and-path-apache-modrewrite-and-php/&nbsp;RewriteRule ^company/([A-Za-z0-9-\s()&+']+)/([A-Za-z0-9-\s()+&']+)&nbsp; test.php?company=$1&name=$2 [NE,B,L]&nbsp;这将导致array(2) {&nbsp; ["company"]=>&nbsp; string(13) "worldwideinda"&nbsp; ["name"]=>&nbsp; string(20) "Durgesh+Jaiswal+&+Co"}由于优点,这不是很好看。最好让 url 编码用正确的转义码 %20 替换它们。你可以用rawurlencode做到这一点
打开App,查看更多内容
随时随地看视频慕课网APP