Mobile app version of vmapp.org
Login or Join
Shanna517

: Remove everything after the question mark in the URL? 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?

@Shanna517

Posted in: #QueryString #Redirects

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.

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Shanna517

2 Comments

Sorted by latest first Latest Oldest Best

 

@BetL925

Even easier:

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

10% popularity Vote Up Vote Down


 

@Alves908

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 (.*) /? [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/)$ /? [R=301,L]

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme