I had a project where I needed to redirect a URL based on the presence or absence of a URL parameter.
If the URL did not include a URL parameter of email
, I had to redirect to another form. We were using Apache as the server, so I could do this with an .htaccess
rule.
Specifically,
# Only proceed with the following rule, if "email=" does NOT appear in the URL query string.
RewriteCond %{QUERY_STRING} !email=
# If we are on the page "my-page-needs-email" (and "email=" is missing)
# perform a 302 redirect to https://example.com/page-to-load-when-no-email
# and stop processing this file (`L`)
RewriteRule ^my-page-needs-email$ https://example.com/page-to-load-when-no-email [R=302,L]
Leave a Reply