Question

I've got a site which at some point in the past used SEO friendly category pagination for example :

category/subcategory/40.html
category/subcategory/4.html
category/subcategory/15.html
category/subcategory/45.html
category/10.html
category/34.html
category/45.html
category/65.html

The site has since reverted to the default functional URL's such as :

  category/subcategory?p=40
    category/subcategory?p=4
    category/subcategory?p=15
    category/subcategory?p=45
    category?p=10
    category?p=34
    category?p=45
    category?p=65

Could anyone help with any possible rewrite rules that wouldn't conflict with any other part of the site, but would capture and 301 the previously used to the current?

Thanks in advanced.

Was it helpful?

Solution

By leveraging web server capabilities on working with .htaccess rules and having RewriteRule module enabled, you could write a rule as the following:

RewriteRule ^/?([^/]+(?:/[^/]+)?)/(\d+)(?:\.\d+)?\.html$ $1?p=$2 [L,R=301]

Regex breakdown:

  • ^ Assert beginning of path
  • /? Match an optional slash mark
  • ( Start of 1st capturing group
    • [^/]+ Match anything but a slash mark (at least one character)
    • (?:/[^/]+)? Repeat previous match with a leading slash mark (optional)
  • ) End of 1st capturing group
  • / Match a mandatory slash mark
  • (\d+) Match one or a sequence of digits (and hold it in 2nd capturing group)
  • (?:\.\d+)? Match an optional decimal part
  • \.html Match .html
  • $ Assert end of path

Now that it stores two required values in two separate groups we can use them in our replacement string:

$1?p=$2

$1 refers to first capturing group value and $2 to the second one. This rule matches e.g.

category/sub-category/65.5.html

and redirects to:

category/sub-category?p=65
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top