0

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.

6
  • Are you using magento? which version? Commented Nov 22, 2018 at 21:18
  • Sorry should have said, Magento 1.9.2.4
    – Randomer11
    Commented Nov 25, 2018 at 18:31
  • Are you looking for a solution within Magento or a workaround at web server app is welcome as well? In the second case, please specify your web server and its version.
    – revo
    Commented Nov 29, 2018 at 12:31
  • I assumed it would be a case of some rewrites for the htaccess ragex , server is running lightspeed.
    – Randomer11
    Commented Nov 29, 2018 at 12:43
  • Try RewriteRule ^/?([^/]+(/[^/]+)?)/(\d+)\.html$ $1?p=$3 [L,R=301] (if you are sure .htaccess rules are being processed currently)
    – revo
    Commented Nov 29, 2018 at 16:33

1 Answer 1

1
+50

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
2
  • 1
    Awesome thats exactly what I was looking for thank you Revo, and thank you for such an indepth answer. I've marked your answer as correct and will release the bounty in a few hours when it becomes releasable
    – Randomer11
    Commented Nov 30, 2018 at 9:04
  • You are welcome.
    – revo
    Commented Nov 30, 2018 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.