sparky_hughes sparky_hughes - 20 days ago 5
PHP Question

Problems with / in URL

I am maintaining an e-commerce system which uses catalogues supplied by various manufacturers to display products and product data. When selecting a product to view the url is

site-address/catalogue/product-category1/product-category2/product-code
.

We have now been tasked with integrating a new supplier whose product codes include
/
in them. This is obviously causing problems because the
/
in cutting off the product code and therefore no page is found.

I can not remove the offending character on catalogue import as the finished order is exported to a back office system local to the customer, which then creates and transmits a purchase order at the end of the day, and if the product code has been altered the suppliers system doesn't recognize the code.

We are using php, mariadb, ElasticSearch and twig template engine.
To save repeated data lookups the catalogues are cached on first arriving at the site.

Answer

You have to encode the '/' character in the url using ASCII notation. Use %2F to represent the '/' character in ASCII.

For example if your product code is 'prod/23/89' then your url will be "site-address/catalogue/product-category1/product-category2/prod%2F23%2F89".

Since you are using php, familiarize yourself with the rawurlencode() function which encodes urls with such characters.