• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Dev Tips / htaccess Redirect Based on URL Parameter

htaccess Redirect Based on URL Parameter

Last updated on January 21, 2019 by Sal Ferrarello

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]
Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit
Warning! This is a draft, not a finalized post. See full draft disclosure.

Filed Under: Dev Tips, Draft, Solution Tagged With: apache, redirects

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in