Setting referrer in a .htaccess redirect with parameter override
December 16, 2016 10:22 AM Subscribe
I've recently moved some of my sites from subdomains, into folders on a single domain, and am trying to maintain as much referrer information as possible through a redirect (including manually passing values via url). I'm not great with .htaccess though, and my current slipshod solution could be better.
Hello hello,
Here's an example of what I'm using now:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^hirsute\.amorphia\-apparel\.com$
RewriteRule ^(.*)$ https://amorphia-apparel.com/hirsute/$1?referer=%{HTTP_REFERER} [R=301,L]
Which has two flaws (I'm aware of) I'd like to fix:
1.) I sometimes pass manual referrers, via the referer parameter, and I'd like to keep those intact. So if I have an old ad that sends the user to "http://controversy.wearscience.com?referer=reddit-ad" i'd like the referer paramter to be maintained as "reddit-ad" instead of %{HTTP_REFERER}.
2.) In the current situation if the %{HTTP_REFERER} field is blank it still tags the link with an empty ?referer= which just annoys me for some reason.
So I'd like my flow to be:
1.) check if the request matches "hirsute.amorphia-apparel.com"
2.) does this currently have a referer parameter I manually set in the url?
- If so pass that as the referer parameter query string with the redirect.
- if not:
3.) is the %{HTTP_REFERER} set?
- If so pass the %{HTTP_REFERER} as the referer parameter query string with the redirect.
- if not:
4.) redirect to the new location without any referer parameter query string.
My googling hasn't turned up results for checking the existence of things like a parameter or, %{HTTP_REFERER} instead it seems like mostly people want to check for a specific value of parameter, or %{HTTP_REFERER} which doesn't really help me in my situation.
Thanks!
Hello hello,
Here's an example of what I'm using now:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^hirsute\.amorphia\-apparel\.com$
RewriteRule ^(.*)$ https://amorphia-apparel.com/hirsute/$1?referer=%{HTTP_REFERER} [R=301,L]
Which has two flaws (I'm aware of) I'd like to fix:
1.) I sometimes pass manual referrers, via the referer parameter, and I'd like to keep those intact. So if I have an old ad that sends the user to "http://controversy.wearscience.com?referer=reddit-ad" i'd like the referer paramter to be maintained as "reddit-ad" instead of %{HTTP_REFERER}.
2.) In the current situation if the %{HTTP_REFERER} field is blank it still tags the link with an empty ?referer= which just annoys me for some reason.
So I'd like my flow to be:
1.) check if the request matches "hirsute.amorphia-apparel.com"
2.) does this currently have a referer parameter I manually set in the url?
- If so pass that as the referer parameter query string with the redirect.
- if not:
3.) is the %{HTTP_REFERER} set?
- If so pass the %{HTTP_REFERER} as the referer parameter query string with the redirect.
- if not:
4.) redirect to the new location without any referer parameter query string.
My googling hasn't turned up results for checking the existence of things like a parameter or, %{HTTP_REFERER} instead it seems like mostly people want to check for a specific value of parameter, or %{HTTP_REFERER} which doesn't really help me in my situation.
Thanks!
Response by poster: Awesome, thanks so much!
For the life of me I couldn't figure out !^$ as the way to check for an empty result, but now it seems obvious. And not knowing what I'm doing I kept trying to fiddle around with it as a set of RewriteRules following a single RewriteCond, and obviously that approach was not working, but this works great.
I just switched the line: RewriteRule ^(.*\?referer=(reddit-ad|some-other-ad))$ https://amorphia-apparel.com/hirsute/$1 [R=301,L]
to: RewriteRule ^(.*\?referer=.*)$ https://amorphia-apparel.com/hirsute/$1 [R=301,L]
To avoid hard coding in all of the various referer parameters I've used over the years, and it seems to work like a charm.
posted by Jezztek at 2:44 PM on December 16, 2016
For the life of me I couldn't figure out !^$ as the way to check for an empty result, but now it seems obvious. And not knowing what I'm doing I kept trying to fiddle around with it as a set of RewriteRules following a single RewriteCond, and obviously that approach was not working, but this works great.
I just switched the line: RewriteRule ^(.*\?referer=(reddit-ad|some-other-ad))$ https://amorphia-apparel.com/hirsute/$1 [R=301,L]
to: RewriteRule ^(.*\?referer=.*)$ https://amorphia-apparel.com/hirsute/$1 [R=301,L]
To avoid hard coding in all of the various referer parameters I've used over the years, and it seems to work like a charm.
posted by Jezztek at 2:44 PM on December 16, 2016
« Older Where do we move to next? Software Developer... | I just can't take this anymore, but Newer »
This thread is closed to new comments.
For the second situation, does the pair of checks (check the host and referer) work?
--
RewriteEngine on
RewriteCond %{HTTP_HOST} ^hirsute\.amorphia\-apparel\.com$
RewriteRule ^(.*\?referer=(reddit-ad|some-other-ad))$ https://amorphia-apparel.com/hirsute/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^hirsute\.amorphia\-apparel\.com$
RewriteCond %{HTTP_REFERER} !^$
RewriteRule ^(.*)$ https://amorphia-apparel.com/hirsute/$1?referer=%{HTTP_REFERER} [R=301,L]
RewriteCond %{HTTP_HOST} ^hirsute\.amorphia\-apparel\.com$
RewriteRule ^(.*)$ https://amorphia-apparel.com/hirsute/$1 [R=301,L]
posted by UrbietOrbi at 2:11 PM on December 16, 2016