.htaccesshttp-redirectparametersrulespermanent

How to implement 301 permanent redirection using htaccess with custom parameter in URL?


Here I have a URL like www.abc.com/product/women/casual/page:5/ and I need to implement 301 permanent redirection using htaccess in order to change the URL to www.abc.com/product/women/casual/page/5/. In this case, parameter women and casual is customized category and subcategory and page:5 is page number 5. I need to change the last parameter page:5 to page/5 using htaccess 301 permanent redirection. Can anyone please help me to find a solution for the case.


Solution

  • You can use the following redirect :

    RedirectMatch 301 ^/([^:]+):5/$  /$1/5/
    

    Or a more generic one:

    RedirectMatch 301 ^/([^:]+):([0-9])/?$ /$1/$2
    

    This will redirect your old URL to the new one , for example example.com/foo/bar:digit/ to example.com/foo/bar/digit/ .

    Or you can use RewriteRule directive

    RewriteEngine on
    
    RewriteRule ^([^:]+):5/$ /$1/5/ [R=301,L]