Search code examples
regex.htaccesshttp-redirecturl-rewriting

.htaccess : Removing unwanted symbols from the end of the URL


example.com/category/fruits">unwanted
example.com/subcategory/apple">&Fweds

I want to remove unwanted symbols which are after fruits* so that it becomes:

example.com/category/fruits
example.com/subcategory/apple

I tried but does not help me :

#removing unwanted symbols from the end
RewriteEngine on 
RewriteRule ^(category/(.*)/[\w-]++). /$1 [R=301,L,NC,NE] 


RewriteEngine on 
RewriteRule ^(subcategory/(.*)/[\w-]++). /$1 [R=301,L,NC,NE] 

Solution

  • This single rule should work for both the cases:

    RewriteRule ^((?:sub)?category/[\w-]++). /$1 [R=301,L,NC,NE] 
    

    Remember there is only one / after category or subcategory before matching fruits which is matched using possessive quantifier [\w-]++ to disallow backtracking.

    Or if category is just a placeholder:

    RewriteRule ^((?:subcategory|category)/[\w-]++). /$1 [R=301,L,NC,NE]