2

Is there any way to redirect a page with a question mark in the URL, to the same page, but with everything after the question mark removed?

For example:

From: /residential-properties/projectname/?utm_source=moneycontrol&utm_medium=email&utm_campaign=project

To: /residential-properties/projectname/

I hope you get my question.

1
  • Understanding why you want to do this would help us answer with a potentially more effective solution. Otherwise this is an XY question. For example, you might be better off just updating URL Parameters instead. Commented Sep 16, 2015 at 17:21

2 Answers 2

2

A literal question mark in the URL marks the start of the query string, so you can test if the query string contains anything and redirect if it does. Using Apache mod_rewrite in .htaccess:

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

The RewriteRule pattern matches against the URL-path, which notably excludes the query string.

The ? (question mark) on the end of the RewriteRule substitution effectively removes the query string from the rewritten URL by creating an empty query string. The question mark itself does not appear in the resulting URL. If you are on Apache 2.4+ then you can use the QSD flag instead (Query String Discard).

If you only need to do this for a specific URL, then change the RewriteRule accordingly:

RewriteRule ^(residential-properties/projectname/)$ /$1? [R=301,L]
2

Even easier:

RewriteCond %{QUERY_STRING} .
RewriteRule ^ %{REQUEST_URI}? [R=301,L]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.