Page 2 of 3

Re: Browser not accepting "+" in the title in the URL

Posted: Thu Jan 28, 2016 3:31 pm
by simonmlewis
If I use [L] rather than [QSA] that still fails.
But as I say, the current site has over 100 products with + in the title and they all work fine. But now... with this new structure, they don't. On the product page, I've not added anything to make it 301.

I've found what is making it do it, but not 'why' it is doing it.

I have this script in an external included file, that takes the page back to a subcategory if the product doesn't exist. And it checks that the product title is correct.

Maybe it's this?? As this is not on the LIVE version. If it is this, how can I get around the problem?

Code: Select all

<?php
  $myprod = isset($_GET['product']) ? $_GET['product'] : null;
  if (isset($myprod))
  {
  // gather $h which has the title in the url, and remove the hyphen.
  $h = isset($_GET['h']) ? $_GET['h'] : null;
  $h = str_replace("-"," ",$h);
  
  $sname = isset($_GET['sname']) ? $_GET['sname'] : null;
  $cname = isset($_GET['cname']) ? $_GET['cname'] : null;
  
  // check if the product ID exists at all
  $queryid = "SELECT id, catid, subid, catname, subname, title FROM products WHERE id = :myprod";
  $stmtid = $pdo->prepare($queryid);
  $stmtid->execute(array(':myprod' => $myprod));
  // if it doesn't exist, 301 to the URLs subcategory page (let the subcategory page handle it beyond that
  if ($stmtid->rowCount() == 0)
    {
    header("Location: /subcateg/$cname/$sname", TRUE, 301);
    exit();
    }
  else
  {
  // if the ID is correct, it then checks if the title is correct.  If it is NOT right, it rewrites it from the database.
  // else it just ignores this whole thing and passes it through
  $query = "SELECT id, catid, subid, catname, subname, title FROM products WHERE title = :h";
        $stmt = $pdo->prepare($query);
        $stmt->execute(array(':h' => $h));
        if ($stmt->rowCount() == 0)
        {
         while ($row = $stmtid->fetch(PDO::FETCH_OBJ)) 
           {
           $title = str_replace(" ","-",$row->title);
            header("Location: /product/$cname/$sname/$row->id/$title", TRUE, 301);
            exit();
           } 
        }
    }
}
?>

Re: Browser not accepting "+" in the title in the URL

Posted: Thu Jan 28, 2016 3:42 pm
by Celauran

Code: Select all

  // if the ID is correct, it then checks if the title is correct.  If it is NOT right, it rewrites it from the database.
  // else it just ignores this whole thing and passes it through
  $query = "SELECT id, catid, subid, catname, subname, title FROM products WHERE title = :h";
        $stmt = $pdo->prepare($query);
        $stmt->execute(array(':h' => $h));
        if ($stmt->rowCount() == 0)
        {
         while ($row = $stmtid->fetch(PDO::FETCH_OBJ))
           {
           $title = str_replace(" ","-",$row->title);
            header("Location: /product/$cname/$sname/$row->id/$title", TRUE, 301);
            exit();
           }
        }
    }
Looks to me like this is the problem. + in URI is being interpreted as a space (see my var_dump earlier) so title is "wrong", being rewritten, redirecting the user, and the cycle repeats.

Re: Browser not accepting "+" in the title in the URL

Posted: Thu Jan 28, 2016 3:48 pm
by simonmlewis
.. and no way to say if it does find a +, leave it there? We have products called something like:

Product 1+
Product 1++
Product 1

If they remove all thsoe plusses, they are a bit knackered.

Re: Browser not accepting "+" in the title in the URL

Posted: Thu Jan 28, 2016 3:50 pm
by Weirdan
Maybe it's this?? As this is not on the LIVE version. If it is this, how can I get around the problem?
Quite likely. Imagine that plus sign gets converted into space somewhere. This script checks the title, finds that it should contain plus and not the space it got, and issues 301 redirect. Browser requests the new url (with plus sign), it gets converted into space and it gets to this script again... ad infinitum. Eventually browser grows tired and complains.

Add

Code: Select all

var_dump($title); die();
, comment out title redirect and post the results.

Re: Browser not accepting "+" in the title in the URL

Posted: Thu Jan 28, 2016 3:54 pm
by Celauran
You could leave them off, urlencode them, come up with separate slugs for products; there are many ways to work around this.

Re: Browser not accepting "+" in the title in the URL

Posted: Thu Jan 28, 2016 3:58 pm
by simonmlewis
IS there a way to urlencode this particular script... rather than me going thru hundreds and hundreds of existing links on the site?

Re: Browser not accepting "+" in the title in the URL

Posted: Thu Jan 28, 2016 4:01 pm
by Celauran
You'd be urlencoding the title, so instead of cat-+-mouse, you'd have cat-%2B-mouse. Not really much better.

Re: Browser not accepting "+" in the title in the URL

Posted: Thu Jan 28, 2016 4:01 pm
by Celauran
You could, however, very easily programmatically generate slugs for all the products.

Re: Browser not accepting "+" in the title in the URL

Posted: Thu Jan 28, 2016 4:10 pm
by simonmlewis
how.....??

Re: Browser not accepting "+" in the title in the URL

Posted: Thu Jan 28, 2016 4:16 pm
by Celauran
Could be something as simple as that. Add conditions as you need. Could even catch the + and replace with 'plus' if you're so inclined.

Code: Select all

function slugify($title)
{
    return str_replace(' ', '-', strtolower($title));
}

Re: Browser not accepting "+" in the title in the URL

Posted: Thu Jan 28, 2016 4:18 pm
by simonmlewis
So you are suggesting rather than using

Code: Select all

$title = str_replace(" ","-",$row->title);
To use that instead? Would that solve it?

Re: Browser not accepting "+" in the title in the URL

Posted: Thu Jan 28, 2016 4:24 pm
by Celauran
That's not what I'm suggesting, no. I'm suggesting adding a column specifically for slugs. You can keep the title intact, slugify all the product titles, keep bad characters out of slugs, and perform your lookup against slug rather than title.

Re: Browser not accepting "+" in the title in the URL

Posted: Thu Jan 28, 2016 4:33 pm
by simonmlewis
Good heavens I've no idea how to do that.
All our links are just rewritten to add the - for a space. Then the page loads.
So not sure how I would do that, without going thru every single page.

All because of this little script I posted. Pity that script cannot be altered to allow the + through as a +. Since it is in there where it dislikes it.

Re: Browser not accepting "+" in the title in the URL

Posted: Thu Jan 28, 2016 4:38 pm
by simonmlewis
I guess what is happening is without that script running, it just passes it through.
But when it does run, it see the - as a -, and the + as a space.
So maybe, I could at the top do something like this:

Code: Select all

$h = isset($_GET['h']) ? $_GET['h'] : null;
  $h = str_replace(" ","+",$h);
  $h = str_replace("-"," ",$h);
Might not work, and I will test it after submitting this... but in theory, a + is the only thing causing a space.... so I could put it back?!

edit: it works!
Any flaws?

Re: Browser not accepting "+" in the title in the URL

Posted: Thu Jan 28, 2016 4:40 pm
by Celauran
simonmlewis wrote:Good heavens I've no idea how to do that.
Sure you do.

Add the slug column to your products table.
Query all products.
Loop through them, passing the title to some slugify function and store its return value.
Update products.

Once that's done, you just modify your query that currently checks for title to check for slug instead.