1

I need to remove "trash" from the end of the URL.

I have more than 1000 URLs with this word: "card link" at the end of the URL.

The URL before the word "card link" is dynamic.

URL example with the trash:

  • https://example.com/frases/f/como-fazer/Card%20link
  • https://example.com/frases/f/just-now/Card%20link
  • https://example.com/frases/f/mybag/Card%20link

URL final:

  • https://example.com/frases/f/como-fazer/
  • https://example.com/frases/f/just-now/
  • https://example.com/frases/f/mybag/
1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented Jan 18, 2023 at 14:26

1 Answer 1

0
https://example.com/frases/f/como-fazer/Card%20link

The Card%20link (aka "trash") that needs to be removed is at the end of the URL-path (not the query string as mentioned in the other answer).

You can remove this using mod_rewrite, near the top of the root .htaccess file.

For example:

RewriteEngine On

RewriteRule ^(.+/)?Card\slink$ /$1 [NC,R=301,L]

Note that the URL-path matched by the RewriteRule pattern has already been %-decoded (ie. URL decoded) so we need to match against a literal space, not %20 (an encoded space). In the above regex, the shorthand character class \s matches any whitespace character.

The regex ^(.+/)?Card\slink$ matches any URL-path that ends with /card%20link (case-insensitive), including requests that should otherwise be for the homepage (if that is a thing, eg. example.com/Card%20link).

You do not need to repeat the RewriteEngine directive if this already occurs elsewhere in the config file. (Although for readability, the RewriteEngine directive should appear just once at the top of the file.)

You should first test with a 302 (temporary) redirect to avoid potential caching issues. And you will need to clear your browser cache before testing.

3
  • 1
    Works ! i just remove the / before $1: RewriteRule ^(.+/)?Card\slink$ $1 [NC,R=301,L] Because the subdirectory. Commented Jan 24, 2023 at 21:44
  • @AdamoSantos Glad you got it working. Ah, so the .htaccess file is located in a subdirectory and you have defined a RewriteBase with the relevant subdirectory? (I had assumed your .htaccess file was located in the root directory.)
    – MrWhite
    Commented Jan 24, 2023 at 22:51
  • @AdamoSantos If this answered your question, then please mark it as "accepted" (tick/checkmark on the left below the voting arrows) to remove it from the unanswered question queue and to help other readers. Once you have 15+ rep then you can also upvote answers you find useful. Thanks, much appreciated. :)
    – MrWhite
    Commented Jan 24, 2023 at 22:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .