Capture referer in httpd access logs after a ReWriteRule - apache

We have two domains and one is primarily for marketing purposes to see how our campaigning works. The marketing domain is redirected to the primary domain. In the httpd.conf, we have a virtual host defined for each of the domain that we host and here is how we redirect the marketing domain to primary domain:
<VirtualHost *:80>
.....
RewriteEngine on
RewriteRule ^/(.*)$ http://primarydomain.com/products [L,R=301]
</VirtualHost>
The redirection works fine, but in the access log, the "referer" is blank. We use AWStats to analyse our web traffic by looking at the access log and without the referer it is hard for us to say how many people landed on the primary domain through the marking domain link. Any recommendations on how to get the "referer" information passed in as part of the RewriteRule so that it is recorded in the access log?
Thanks!

Related

Redirect non-www to www https Apache XAMPP

Please forgive what may sound like a similar question to what has been asked, however, I am VERY new to XAMPP and Apache and I have tried all possible combinations of the Rewrite Rules mentioned in other threads, which I have placed in the httpd.conf, httpd-default.conf and also in .htaccess and I simply cannot get the rewrite rules to work.
I simply want to redirect example.com to www.example.com. Please note that my redirect from HTTP to HTTPS is working 100%, so if someone can please advise on exactly where I should place the rewrite rule, and which one to use, to force non-www to www, I would be most appreciative.
I have full access to the server so I can edit either .conf or .htaccess files. Also I have checked and the part in httpd.conf that allows overrides to read the .htaccess files is enabled. The .htaccess file is currently in the same folder where my website lies. Just to add, I don't get any error, it just says the page has timed out if I type in example.com, if I type www.example.com then the page loads as expected.
Try this in your .htaccess file:
Replace example.com with your url.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC]
The connection has timed out
This is a DNS or server config error. The request is not even reaching your server, or your server is not responding to the request. (So any attempt to implement a redirect will naturally fail.)
A quick check of the DNS for example.com and www.example.com shows they are pointing to different IP addresses (you have an A record configured for each). The example.com host is 1 digit different and would appear to be incorrect (the IP address does not respond to requests, hence the "time out").
Instead of having a separate A record for each hostname. If the www subdomain is supposed to point to the same site as the domain apex (ie. example.com) then configure a CNAME record instead that points www.example.com to example.com (this is not a "redirect").
You then need to ensure that your server will accept requests to the non-www hostname. For example:
ServerName example.com
ServerAlias www.example.com
(It's possible you have the ServerName and ServerAlias reversed - which is "OK".)
And the SSL cert needs to cover the non-www hostname (it does).
You can then implement your non-www to www HTTP redirect as mentioned.

vhost rewrite sudomain to subfolder/file

I've read countless examples but nothing quite like what I'm trying to do.
Is it possible to use a vhost to redirect traffic sent to a subdomain to a main domain/subfolder/file.html?
Example:
User types into their browser http://productname.example.net/. I'd like them to end up at https://example.net/category/productname.html
NOTE: The user may or may not enter httpS but the ending site is an SSL url.
My conf file is as follows:
<Directory C:/websites/productname.example.net>
#BestProduct.example.net
#https://www.example.net/category/BestProduct/
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} bestproduct\.example\.net
RewriteRule (.*) /category/bestProduct/$1 [L]
</Directory>
The example.net has a different conf file because it is an SSL. I have a couple of websites running from the same server so I have several conf files.
My TLD DNS Entry: A Record is 123.456.789 (SSL URL)
My subdomain of BestPRoduct is 123.456.780 (non SSL URL) Is this correct? If I point this tot he SSL IP, the browser reports it as unsafe. The TLD redirects www.example.net to just example.net.
What do I need to change to make this scenario work as I've described? I'm stumped.
UPDATE
When I reviewed the access.log and the error.log file for the subdomain, it did not have any records in it (file size was 0 bytes). This makes me think that perhaps the DNS record is not setup correctly?
FINAL UPDATE
During the course of just a casual conversation with the client, I found out they want to do this with just about every product they sell (jaw drop). Apparently this client was informed by some SEO firm to setup subdomains for better SEO rankings. I recommended to the client to setup subfolders instead of subdomains and ultimately that is what we are doing. My argument was from a maintenance standpoint that setting up subdomains for every product wasn't feasible.
Q: User types into their browser http://productname.example.net/. I'd like them to end up at https://example.net/category/productname.html
A: Use Redirect. For example
<VirtualHost *:80>
ServerName productname.example.net
DocumentRoot "C:/websites/productname.example.net"
Redirect permanent "/" "https://example.net/category/productname.html"
</VirtualHost>
Notes
To redirect https see HOWTO: Apache Name-Based SSL-Enabled Virtual Hosting.

Rewrite address bar without redirecting

I have a host in my university's http server under the domain: university.com/~username
Now, I also have a domain on: mydomain.net with GoDaddy
I want to know if it is possible to have a subdomain:
university.mydomain.net that basically redirects to
university.com/~username.
Now, the trick here is that if I want to access
university.com/~username/subdir via university.mydomain.net/subdir
the address bar in the browser shows: university.mydomain.net/subdir no
matter if I access it via university.com/~username or university.mydomain.net
The problem with using permanent redirect with masking in GoDaddy, then if
I am in the dir university.mydomain.net/subdir and go to the subsubdir
university.mydomain.net/subdir/subsubdir then the browser still shows university.mydomain.net/subdir
And about CNAMEs or other kind of register in GoDaddy, I really have no idea
what to do (this is my first domain) and I don't even know if I have the rights
in my university server to make a difference.
I tried with a RewriteRule in a .htaccess file, but this always tries to
redirect (no matter what flag I use) to university.mydomain.com/subdir
while the only thing I want to do is that the address bar shows the direction
without following it.
I really don't even know if this is possible somehow. Sorry if it's an stupid
question, I'm very new with domains and those stuffs.
Under the CNAME you need to add you subdomain entry (in the Godaddy DNS management console)
university.mydomain.net points to #
and in your host the # might be pointing to the IP address where your university server is running.
Then in your apache vhost config, you would need to have rewrite rule like
<VirtualHost *:80>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^university.mydomain.net(:80)?$
RewriteRule ^/(.*) http://university.com/~username/$1 [P,QSA,L]
RewriteCond %{HTTP_HOST} ^university.com(:80)?$
RewriteRule ^/~username/(.*) http://university.mydomain.net/$1 [R=301,L]
</VirtualHost>

Rewrite Apache directive for a 301 redirect

There is a malicious site which has replaced mine in Google search results. I checked the headers and this malicious domain is sending traffic directly to our servers.
I'd like to rewrite Apache's directives so that there is a 301 redirecting any traffic from the malicious site to our site in order to get Google to give me back my listing.
Does anyone know how to write this directive? I hardly know anything about this stuff but I think it involves RewriteEngine. Do I update it in .htaccess?
UPDATE FOR CLARITY: When someone searches for my company name in Google the malicious domain is the top result. Click on it and it immediately goes directly to our server - it's not a redirect, it's a straight 200 to my IP (but in their domain). Makes me think they're pointing their DNS to my IP. Is there a way to write a directive that would execute a 301 redirect to my domain whenever traffic was coming from the malicious domain?
RESOLVED: I had help figuring this out... here's the fix. I put this at the top of my {site.com}.conf file:
<VirtualHost *:80>
RedirectMatch permanent ^/(.*) http://www.{site}.com/$1
</VirtualHost>
You say this:
I checked the headers and this malicious domain is sending traffic
directly to our servers.
Then this:
I'd like to rewrite Apache's directives so that there is a 301
redirecting any traffic from the malicious site to our site in order
to get Google to give me back my listing.
So it’s already sending traffic to your servers but you want to do what exactly? Apache rewrite rules only work on servers you have control of. If someone poisoned your listings on Google & now gets traffic, your access to your own servers won’t change that.
I had help figuring this out... here's the fix. I put this at the top of my {site.com}.conf file:
<VirtualHost *:80>
RedirectMatch permanent ^/(.*) http://www.{site}.com/$1
</VirtualHost>
test the host header and redirect.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?yourhost.com$
RewriteRule . http://www.yourhost.com%{REQUEST_URI} [R=301]

Apache mod_rewrite ANY subdomain(s) to root domain, unless existent as virtualdocumentroot

Say you've got an Apache2 virtual host setup, something like this:
/htdocs/parent1.com
/htdocs/sub1.parent1.com
/htdocs/sub2.parent1.com
/htdocs/parent2.net
/htdocs/parentn.org
Say you'd like to do this with VirtualDocumentRoot /htdocs/%0, so that you can add and remove virtual hosts without tinkering with your Apache configuration. That's important: please please no messing with htaccess files or httpd.conf every time a virtual host comes or goes - whether that host is a parent domain or not. In fact, say you're using AllowOverride None.
Anyway, the question is, how might you 301 redirect non-existent sub-domains to their corresponding parent domains without redirecting existent sub-domains?
I may have solved my own problem. However I would appreciate any feedback if somebody finds a problem with what I'm doing.
The following leaves alone any request to an arbitrary subdomain, as long as there exists a corresponding document root; but redirects any request to a subdomain which does not exist in the filesystem.
<IfModule rewrite_module>
RewriteEngine On
RewriteMap lowercase int:tolower
RewriteCond "/htdocs/${lowercase:%{HTTP_HOST}}" !-d
RewriteCond %{HTTP_HOST} "\.([^\.]+\.[^\.]+)$"
RewriteRule ^/(.*)$ "http://%1/$1" [R=301]
</IfModule>
Allows me to setup wildcard DNS and use name-based virtual hosting, without touching any configuration settings. Also, there's no htaccess involved. Just make your folder with any name like "/htdocs/[host.]domain.tld" and you're up and running. As far as I can tell, this doesn't really work with SSL/TLS (presumably something to do with %{HTTP_HOST}?), but secure sites are comparably few and better resolved by IP address than by hostname.