I have a new domain for my site and want to forward the user to this new site.
So I set up a simple redirect in my .htaccess file likeI have a new domain for my site and want to forward the user to this new site.
So I set up a simple redirect in my .htaccess file like this:
RewriteCond %{REQUEST_URI} (.*)
RewriteCond %{HTTP_HOST} ^www.oldsite.com$
RewriteRule ^(.*)$ https://www.newsite.com%{REQUEST_URI} [L,R=301]
That works so far, but now I need to check if a user still uses the old domain and therefore I need to check the http_referrer in my php application. Unfortunately the http_referrer is not set? How can I tell my .htaccess to set the http_referrer to www.oldsite.com if the RewriteCond triggers?
Best Answer
tl;dr To check to see whether
oldsite.com
is still being requested, you should check your server access logs. (Maintain a separate log, just foroldsite.com
.)You can't. The
Referer
HTTP request header is set by the User-Agent / browser. And the redirect itself is not a referrer.For example:
If you type
oldsite.com
directly into the browser (direct request, ie. noReferer
header is set) thenoldsite.com
issues a redirect tonewsite.com
then the original "no referrer" is preserved and noReferer
header is sent.If you click on a link to
oldsite.com
fromexample.com
thenexample.com
is set as theReferer
(by the browser). Ifoldsite.com
then issues a 301 redirect tonewsite.com
thenexample.com
is still the referrer and this is preserved by the browser. The intermediary site that issues the redirect is effectively hidden from the target site.Note that if
Referer
headers are set (and what they are set to) is also dependent on any referrer-policy that might be set on the originating site (as well as any client settings the user might have). The referrer-policy also covers HTTP to HTTPS navigation etc. (which does not send aReferer
by default).You could perhaps append an innocuous URL parameter as part of the redirect and check for this in the receiving script. This does, however, expose this URL parameter - which you would need to make sure is not picked up by search engines etc. so as not to hamper SEO (ie. set the appropriate
rel="canonical"
and/or issue a secondary redirect to remove the URL parameter - once you have logged the request).For example:
(The first condition that checked against
REQUEST_URI
wasn't doing anything.)At
newsite.com/.htaccess
you could do something like the following to remove the URL parameter (this could be simplified if you have no other URL parameters). However, you might not want to perform this redirect (or perhaps "delay it" or "set a cookie") if you are wanting to display a message to the user.