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.

5
  • 1
    Add an empty querystring in your destination ? or use the QSD flag if your Apache version supports it. Commented Mar 19, 2019 at 8:09
  • QSD supported by my Apache, but doesn't remove QS... What you mean to add empty querystring? example please Commented Mar 19, 2019 at 8:12
  • See stackoverflow.com/questions/21118511/… Commented Mar 19, 2019 at 8:13
  • Doesn't work... 403 forbidden Commented Mar 19, 2019 at 8:25
  • To be clear when you say ?id=qqq is an invalid querystring that should redirect, do you want ?id=123 to be treated a valid querystring that shouldn't redirect? Commented Mar 19, 2019 at 9:22

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!
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]

Comments

0

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

RewriteRule ^forum_diskuse/ poradna/1/? [R=301,L]

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.