apache rewrite: how do I check for empty or not present query parameter?
might be best explained with an example:
- If I enter either URLs in my brower
- www.myhost.com/my-page.html?year=
- www.myhost.com/my-page.html
- I then want to get redirected to www.my-2nd-host.com/current-year/my-page.html
Any ideas how it's done? Thank you.
You can do it like this using mod_rewrite:
RewriteEngine On
RewriteCond %{QUERY_STRING) !(^|&)year=[^&]+
RewriteRule ^/?my-page\.html$ https://www.my-2nd-host.com/current-year/my-page.html [R=302,L]
The regex (^|&)year=[^&]+
checks for the presence of the year
URL parameter with a non-empty value. The !
prefix then negates the expression so it is successful when that URL param is either empty or not present at all.