Shadow Shoot
Shadow Shoot

Reputation: 27

Having issue with .htacces file and get method in php

I have URL with company.php?name=Son's.&.clothes

when I use .htaccess file it should be like 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

And the output is

I use GET method in PHP to get the name from URL:

 if(isset($_GET['name'])){
        $name = $_GET['name'];    

    }

    var_dump($name);
   Jai Industries Ltd.php 

This is my .htaccess file

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]

The output of $name is like Son's

Upvotes: 0

Views: 77

Answers (1)

Tschallacka
Tschallacka

Reputation: 28722

The url requested isn't urlencoded.

The & character means new parameter in the get request.

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

url?foo=bar&baz=bal

will turn into:

$_GET = [
    'foo' => 'bar',
    'baz' => 'bal'
];

So the same logic applied to your url company.php?name=Son's.&.clothes, then in your case you get

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

or if your webserver/php settings are different, the empty get variable might get dropped alltogether.

urlencode() your urls before rendering them with php, or javascript with encodeURIComponent() or by manually encoding/replacing the ampersand with %26

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

On basis of the edited question

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

Drop the first line. The urlencode will take care of that. Now you're urlencoding the escape sign of the %26. Those two lines should be turned into:

 $url = urlencode($url);

Which will give

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

Now your rewrite rule is nonsensical

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

This checks if an url starts with the word company.
Your url starts with worldsinda
Even if it matches with that by swapping out company for worldsindia then your url would be turned into:

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

which is a weird get request to get. But then because it's a rewrite rule and not a redirect rule, it gets fed into the .htaccessfile from the top again.

So what you now have is the url:

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

Which will interpret the & as a parameter seperator, which will drop the second parameter because it's empty. So you end up with

array(1) {
  ["name"]=>
  string(21) "name/Durgesh Jaiswal "
}

Now to fix it, you need to fix your rewrite rule

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

Here we set the base url /company so anything after company will be fed to the company.php file. Anything that's not a company will be fed to something else.

Now the parameters int he get request are

array(2) {
  ["company"]=>
  string(11) "worldsindia"
  ["name"]=>
  string(16) "Durgesh Jaiswal "
}

We still don't have the part after the ampersand, this is because Apache does stuff, but mostly, because you don't have the ampersand in your redirect rule.

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

Now we end up with:

array(3) {
  ["company"]=>
  string(13) "worldwideinda"
  ["name"]=>
  string(16) "Durgesh Jaiswal "
  ["Co"]=>
  string(0) ""
}

We can fix this by adding the B modifyier to the rewrite rule, read for more information 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] 

Which will result in

array(2) {
  ["company"]=>
  string(13) "worldwideinda"
  ["name"]=>
  string(20) "Durgesh+Jaiswal+&+Co"
}

Which is not nice to see because of the plusses. Better to let the url encoding replace them with the proper escape code, %20. You can do this with rawurlencode working sample

Upvotes: 1

Related Questions