PaaPs
PaaPs

Reputation: 393

how do I redirect a url and remove part of the string

I have an url like this:

domain.com/thispart/blah-something-blah-remove

I need to redirect this url like this:

domain.com/newpart/blah-something-blah

change the directory and remove the last part(the last part is always constant, this time it's remove).

How do I do this? I managed to redirect the directory, but the last par I don't know how to remove.

Upvotes: 0

Views: 330

Answers (1)

MrWhite
MrWhite

Reputation: 45829

Providing you don't have any existing mod_rewrite directives then you can do something like the following using a mod_alias RedirectMatch directive near the top of your .htaccess file:

RedirectMatch ^/thispart/([\w-]+)-remove$ /newpart/$1

Note that this removes "-remove", as in your example, not simply the string "remove".

The path segment before the "-remove" part is assumed to consist of the characters 0-9, a-z, A-Z, _ or -.

This is a temporary (302) redirect.

You will need to clear your browser cache before testing.


However, if you have existing mod_rewrite directives then you should use mod_rewrite instead to avoid potential conflicts (and improve efficiency). For example:

RewriteRule ^thispart/([\w-]+)-remove$ /newpart/$1 [R=302,L]

(Note the absence of the slash prefix on the RewriteRule pattern.)

Upvotes: 1

Related Questions