1

If URL contains a special parameter (URL contains Email in the parameter) redirect to the base URL of the page without any parameters.

For example:

http://example.com/accounts/daily/ybk/?Email=redacted@example.com

to

http://example.com/accounts/daily/ybk/
MrWhite
  • 11,643
  • 4
  • 25
  • 40
adminz
  • 397
  • 2
  • 4
  • 19
  • 2
    Possible duplicate of [Redirect, Change URLs or Redirect HTTP to HTTPS in Apache - Everything You Ever Wanted to Know About Mod\_Rewrite Rules but Were Afraid to Ask](http://serverfault.com/questions/214512/redirect-change-urls-or-redirect-http-to-https-in-apache-everything-you-ever) – Tero Kilkanen Oct 17 '16 at 21:03

2 Answers2

3

In order to match the query string, you'll need to use a RewriteCond (mod_rewrite) directive and match against the QUERY_STRING server variable. For example, in your root .htaccess file try the following:

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

For all URLs that contain Email= anywhere in the query string, 301 redirect to the same URL less the query string.

The ? on the end of the RewriteRule substitution removes the query string from the redirected URL. Alternatively, you can use the QSD flag on Apache 2.4+.

If you've previously tested with erroneous 301s then make sure you clear your browser cache.

UPDATE: For the specific URL stated (including Email= at the start of the query string):

RewriteEngine On
RewriteCond %{QUERY_STRING} ^Email=
RewriteRule ^(accounts/daily/ybk/)$ /$1? [R=301,L]
MrWhite
  • 11,643
  • 4
  • 25
  • 40
  • Alternatively you could use the following for the RewriteRule, `RewriteRule ^ - [L,QSD]`. As you say, requires Apache v2.4. Only really relevant for high volumes of requests. – Unbeliever Oct 18 '16 at 06:31
  • @Unbeliever Although the OP appears to require an _external redirect_ - presumably to canonicalize the request. Removing the query string on an internal rewrite would seem to be unnecessary (assuming the script is able to simply ignore the `Email` parameter). – MrWhite Oct 18 '16 at 08:08
  • you are correct, I missed the [R=301] from the rewrite flags, which is required to keep it the same as your post. Looking at the original question, it's unclear as to whether it needs to be an external redirect or not. – Unbeliever Oct 18 '16 at 08:51
  • @w3dk The above rule works. but in my case can we Implement this rule only for specific URL. Some specific URL comes with Email parameter, not all URL – adminz Oct 18 '16 at 16:06
  • I've updated my answer for the specific URL in your example. – MrWhite Oct 18 '16 at 16:36
0

The mod_rewrite module should be able to help you out. You write the conditions and your rewrite rules into the .htaccess file.

Reference: Using .htaccess rewrite rules

K Richardson
  • 201
  • 2
  • 8
  • 1
    You can't match the _query string_ with the `RewriteRule` _pattern_ - this matches the URL-path only (which notably excludes the query string). But neither would this _remove_ the query string from the request. Unfortunately the resource you link to doesn't appear to cover query strings? – MrWhite Oct 17 '16 at 18:19
  • Ah, my mistake. Thanks for pointing that out. Removed erroneous code. – K Richardson Oct 17 '16 at 20:18