0

The goal - remove query string and redirect to parent url if QS doesn't meet some criteria. So, as an example this url with wrong QS:

https://www.example.com/view.php?id=qqq

Should be redirect to:

https://www.example.com/view.php

So far I have this:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(?!(mk=[1-9][0-9]{0,1}|id=[1-9][0-9]{0,3})$).*$
RewriteRule ^view\.php$ view.php? [L,R]

But it doesn't work, either reports on 403 error, or 500 Internal Server error, or too many redirects, or no site CSS loads etc. while checking it in WAMP.

I tried this also:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(?!(mk=[1-9][0-9]{0,1}|id=[1-9][0-9]{0,3})$).*$
RewriteRule ^view\.php$ - [L]

It redirects well, but doesn't remove query string. Tried with QSD flag too.

Please, help.

IVGSoft S
  • 35
  • 2
  • 9

3 Answers3

1

Try this:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^.+$
RewriteCond %{QUERY_STRING} ((?:^|&)id=([^&]+)|(?:^|&)mk=([^&]+))
RewriteRule ^view\.php$ view.php? [L]

The first part checks to make sure a querystring exists to avoid redirect loops.
The second part checks to see if an id= or mk= parameter exists in the querystring.
The third part then redirects view.php (with or without a querystring) to the same file but with the querystring trimmed off. This might still show a useless question mark in the URL in which case, see if QSD (querystring discard) is any better. Note the L flag for last rule is used. This will prevent any further rewrites for the current iteration. Apache still needs to scan the file from the top again to make sure it doesn't need to rewrite further.

If this is going to encounter a redirect loop from other rewrite rules you're not showing us, might need to utilize a trick like the following at the top of your .htaccess file:

RewriteEngine On

#Break infinite loops
RewriteCond %{ENV:REDIRECT_SKIP_ROOT_HTACCESS_FILE} =1
RewriteRule ^.*$ - [L]


# ...
# more logic can go here
# ...

# trim querystring from bad links
RewriteCond %{QUERY_STRING} ^.+$
RewriteCond %{QUERY_STRING} ((?:^|&)id=([^&]+)|(?:^|&)mk=([^&]+))
RewriteRule ^view\.php$ view.php? [L,ENV=SKIP_ROOT_HTACCESS_FILE=1]

If you need to test for mk=[1-9][0-9]{0,1} or id=[1-9][0-9]{0,3}, the above should be easy enough to edit. Simply replace the instances of ([^&]+) with your numeric logic.

Ultimater
  • 4,647
  • 2
  • 29
  • 43
  • You are the best !!! "The first part checks to make sure a querystring exists to avoid redirect loops." I have added this RewriteCond %{QUERY_STRING} ^.+$ and IT WORKS NOW!, thanks so much! – IVGSoft S Mar 19 '19 at 09:19
1

Your rules are fine excpet this .* which means 0 or more match and that why when you remove the query string looping happened so no need for more lines, you could only change this part .* by this .+ which means 1 or more match like this :

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(?!(mk=[1-9][0-9]{0,1}|id=[1-9][0-9]{0,3})$).+$
RewriteRule ^view\.php$ view.php? [L,R]
Mohammed Elhag
  • 4,272
  • 1
  • 10
  • 18
0

Just adding ? at the end will remove the query string.

RewriteRule ^forum_diskuse/ poradna/1/? [R=301,L]
Martin Zvarík
  • 2,120
  • 22
  • 27