Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Url rewrites #373

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open

Url rewrites #373

wants to merge 16 commits into from

Conversation

johanjanssens
Copy link
Member

@johanjanssens johanjanssens commented Jun 17, 2020

This PR closes #372 and implements a specialised rewrite router for url's generated by the Joomla router.

The url rewrite router offers support for both pattern and callback based routing. It removes the need for complex SEF extensions, allows to easily define custom rewrite rules and handles redirects automatically.

Rewrite routing is available globally, rewrites can be setup outside of the context of Pages for any url on your Joomla site

Adding routes

Routes are defined in the configuration through the new rewrites config option.

<?php
return array(
    'rewrites' => [
           'blog/[id:category]/[id:article]' => 'posts/[slug:article]'
    ]
);

Rewrite redirects

The router will automatically handle redirects from old to new url's for defined rewrites, if the rewrite is updated or removed it will also handle rewrites from new to old url's. This ensure that regardless of the rewrites being setup page authority is retained.

Match types

Following new match types have been added to make it easy to match urls with id's and remove id's from urls.

  • id: 1-my-article-title
  • slug: my-article-title

An id type MUST start with a number followed by a dash - while an slug MUST start with a letter.

Following are all valid id's:

  • 1
  • 1-title
  • 1-long-tile
  • 1-long-title-2

Following are all valid slugs:

  • title
  • long-title
  • long-title-2 //can end on a number
  • 1long-title-2 //can start with a number

Filtering routes

The router is now also capable of filtering route targets. If a target segment includes a constraint it will be filtered accordingly. This allows to transform the url segments easily.

For example, the following rewrite rule will match a segment that starts with an id and filter it leaving only the slug in the rewrite: 'news/[id:article]' => 'news/[slug:article]'

Example: Removing id's

The following rewrite route will remove id's from the url's for the news menu item in Joomla. We are providing the slug constraint here too in the target url, which will filter out the id from the actual slug.

<?php
return array(
    'rewrites' => [
           'news/[id:category]/[id:article]?' => 'news/[slug:category]/[slug:article]?'
    ]
);
  • Original: http://example.com/news/1-company/2-about-covid
  • Rewrite: http://example.com/news/company/about-covid

Example: Shortening url's

The following rewrite route will create a short url, removing the category from the url, but retaining the article id. This ensures the url is unique.

<?php
return array(
    'rewrites' => [
           'blog/[id:category]/[id:article]' => 'blog/[:article]'
    ]
);
  • Original: http://example.com/blog/1-products/2-announcement
  • Rewrite: http://example.com/blog/2-announcement

The following rewrite route will create a short url, removing the category from the url, and also removing the article id. In this case you need to ensure the article slug is unique.

<?php
return array(
    'rewrites' => [
           'blog/[id:category]/[id:article]' => 'blog/[slug:article]'
    ]
);
  • Original: http://example.com/blog/1-products/2-announcement
  • Rewrite: http://example.com/blog/announcement

Global configuration

You do not need to setup a /joomlatools-pages folder to be able to define rewrite routes. If you just want to use this feature add a configuration-pages.php to your Joomla root, and defines your rewrite routes there, they will just work.

This allows pages to be to redirect any url, anywhere, even when not
in a page context.
identifiers by a custom scheme and rename 'pages' scheme to 'page'
The callback is defined is as follows: 'function($route, $generate = false)'

- $route: a ComPagesDispatcherRouteRouteInterface object
- $generate: are we generating a url or resolving (default false)

Callbacks are both supported for static and dynamic routes, in case of a
dynamic route the callback is called only if the route could be succesfully
resolved.
Example:

'/path/to/page' => [
	'generate' => function($route)
	{
		return true;
	},
	'resolve' => function($route)
	{
		return true;
	}
],
- Add 'slug' expression constraint
- If the target contains a contraint the target segment will be filtered acoordingly
- Add 'id' type
- Do not use a match group for 'slug' type
# Conflicts:
#	code/site/components/com_pages/dispatcher/http.php
#	code/site/components/com_pages/resources/config/site.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Url rewrites
1 participant