0

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

  • https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_b – misorude Aug 05 '19 at 13:14
  • Possible duplicate of [htaccess rewrite rule with escaped ampersand in $\_GET fails](https://stackoverflow.com/questions/8467254/htaccess-rewrite-rule-with-escaped-ampersand-in-get-fails) – misorude Aug 05 '19 at 13:14

1 Answers1

1

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

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
  • 1
    ok i used urlencode() and the url comes like this Durgesh+Jaiswal+%26amp%3B+Co but get['name'] is Durgesh Jaiswal only and how to replace %26amp – Shadow Shoot Aug 05 '19 at 12:21
  • @ShadowShoot You're sending `"Durgesh+Jaiswal+&+Co"` There's an html entity in there, that might be blocked for some reason by an overly zealous firewall. Can you try `"Durgesh+Jaiswal+%26+Co"` – Tschallacka Aug 05 '19 at 12:24
  • @ShadowShoot You don't need to, your webserver will translate it to the correct character. And your sent parameter works perfectly as far as I can see https://imgur.com/6AvLwtI – Tschallacka Aug 05 '19 at 12:28
  • but i get only Durgesh Jaiswal not remaining – Shadow Shoot Aug 05 '19 at 12:36
  • What is the entire request you are doing? Are you doing the company.php request or are you doing a /company/whatevever request? Can you paste the url you are using? – Tschallacka Aug 05 '19 at 12:49
  • @ShadowShoot added the answer. – Tschallacka Aug 05 '19 at 13:43
  • ok i used it but when i used dot(.) in rewrite rule it comes Durgesh Jaiswal & Co.php how this possible – Shadow Shoot Aug 05 '19 at 13:51
  • can u check this because i have company name like Jai industries Ltd.(dot) after Ltd in url but come like Jai industries Ltd.php why – Shadow Shoot Aug 05 '19 at 14:04
  • @ShadowShoot Can you please post the full url you're using? and show the rewrite rule you put in your .htaccess? Did you use the one I gave you? do your urls start with localhost/company/companyname? – Tschallacka Aug 05 '19 at 14:12
  • @ShadowShoot You haven't made the rewrite url like I told you, there is no catching anything for company. Also a `.` isn't included in your regex filter `RewriteRule ^company/([A-Za-z0-9-\s\.()&\+']+)/([A-Za-z0-9-\s()\.\+&']+) test.php?company=$1&name=$2 [NE,B,L] ` also make sure your company parameters don't end with a . so Ltd. the dot will bestripped by the webserver/browser, for whatever reason. there needs to be a non space character behind it for it to be included. Also, don't use str_replace but use urlencode. – Tschallacka Aug 05 '19 at 14:47
  • ok used as mention but for company name like Innovation it comes like Innovation.php – Shadow Shoot Aug 05 '19 at 14:56
  • @ShadowShoot You're not using the rewrite rule I gave you, I don't know what you're expecting of me. – Tschallacka Aug 05 '19 at 14:59
  • this is my url localhost/worldsindia/company.php?name=Innovation worldindis is folder ok – Shadow Shoot Aug 05 '19 at 15:02
  • ok can i use my rewrite rule of remove .php extension from url RewriteRule ^([^\.]+)/?$ $0.php [NC,L] – Shadow Shoot Aug 05 '19 at 15:08
  • @ShadowShoot I have no idea where you get the plusses from, but just pass the company name or whatever name to [rawurlencode](https://www.php.net/manual/en/function.rawurlencode.php) example on https://ideone.com/ycRUl1 – Tschallacka Aug 05 '19 at 15:13
  • ok i understand but if i use this rewrite rule RewriteRule ^([^\.]+)/?$ $0.php [NC,L] and your rewrite rule it comes Innovation like innovation .php – Shadow Shoot Aug 05 '19 at 15:25
  • @ShadowShoot please take a look at the last image, the url that is in there. Does it resemble the url format you are using? if not, you're not doing it right. Read the *entire* answer from start to end, and try to understand each step and the logic behind it. – Tschallacka Aug 05 '19 at 15:39
  • worldindia is folder in which company.php is file and you are using worldindia as a file? – Shadow Shoot Aug 05 '19 at 15:49