Redirect url containg a specific string to url bef

2019-08-24 13:08发布

问题:

I have some urls with the following structure :
https://www.example.com/name-of-a-page?specific_string-anything

I would like to redirect such urls to :
https://www.example.com/name-of-a-page

In other words, I would like to redirect urls containing a specific string, to the part of url located before the question mark.

I thank you in advance for any help in this matter.
Patrick

回答1:

A simple regex to get matching that part would be [^\?]+(?=\?).

I can't give you any other information because I don't know what language you like to use.

EDIT:

According to the new information this should work in the .htaccess file:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} ^.+$
RewriteRule ^(.*)$ /$1? [R=301,L]

EDIT 2:

According to the latest information ;) the code should be this:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} ^specific_string.*$
RewriteRule ^(.*)$ /$1? [R=301,L]