Search code examples
regex.htaccessmod-rewriteurl-rewriting

Regex to remove trailing string in .htaccess RewriteRule


My web server is appending unwanted extra characters to the ends of URLs and I would like to remove these. My current RewriteRule is...

RewriteRule ([0-9]{4})\/([0-9]{2})\/([0-9]{2})\/(.*) https://example.com/$4 [R=301,L]

This takes http://example.com/2021/06/19/page-name and converts it to http://example.com/page-name.

The problem with this rule is the wildcard, anything after page-name is also included, such as https://example.com/page-name/%s. How can I modify this rule to omit anything after $4?


Solution

  • With your shown samples, please try following htaccess Rule. Make sure to clear your browser cache before testing your URLs.

    RewriteRule ^\d{4}/\d{2}/\d{2}/([^/]*).*/?$ https://example.com/$1 [R=301,L]
    

    Explanation: You need not to create 4 capturing groups here, just match from starting of url 4 digits/2 digits/2 digits and then capture everything till next occurrence of / in 1st capturing group, which could be used in redirection part.