User Tools

Site Tools


how_to_create_a_custom_route_url

How To Create A Custom URL And Route

When accessing the URL www.site.com/mypage LiteCart looks for a document in the folder pages/ named mypage.inc.php.

You can rewrite the URL and/or you can set up URL aliases.

URL Rewrite Example

In this example we are creating a custom url for mypage depending on the selected language.

includes/routes/url_mypage.inc.php
<?php
  class url_mypage { // The class name url_{pagename} must match the page filename -> pages/{pagename}.inc.php
 
  // For incoming traffic
    function routes() {
      return array(
        array(
          'pattern' => '#^my-fantastic-url-(\d+)$#',  // This is the regex pattern for resolving a rewritten url back to a resource
          'page' => 'mypage',  // This is the target document meaning pages/mypage.inc.php
          'params' => '', // Establish URL query parameters based on the result of the regex. E.g. page_id=$1 etc.
          'redirect' => true, // If accessing a non rewritten url, let's do a 301 redirect
        ),
      );
    }
 
  // Four outgoing traffic
    function rewrite($parsed_link, $language_code) {
 
      switch($language_code) {
        case 'de':
          $parsed_link['path'] = 'mein-fantastisches-url';
          break;
        default:
          $parsed_link['path'] = 'my-fantastic-url';
          break;
      }
 
      return $parsed_link;
    }
  }
?>

Alias Routes Example

includes/routes/url_old_oscommerce_links.inc.php
<?php
  class url_old_oscommerce_links {
 
    function routes() {
      return array(
        array(
          'pattern' => '#^.*-c-([0-9]+).html$#',
          'page' => 'category',
          'params' => 'category_id=$1',
          'redirect' => false,
        ),
        array(
          'pattern' => '#^.*-p-([0-9]+).html$#',
          'page' => 'product',
          'params' => 'product_id=$1',
          'redirect' => false,
        ),
      );
    }
  }
?>

Please note incoming url query parameters are not parsed when processing a page request.

how_to_create_a_custom_route_url.txt · Last modified: 2020/09/30 23:53 by tim