vhost rewrite sudomain to subfolder/file - apache

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.

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.

Redirecting New Domain Name to Server

I recently purchased a new domain name from 1and1.com and used their HTTP redirect option to point to the address of my server. Let's say, for example, the fresh domain is new.com and the established server is old.com.
I have it redirecting to old.com/new via 1and1's configuration page, which works, save for the fact that when I visit new.com, it changes the browser's URL to old.com/new. This is obviously not what I want to happen.
I've set up htaccess rules:
# BEGIN New.com
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^new.com
RewriteRule ^(.*) http://old.com/new [P]
</IfModule>
# END New.com
Likewise, I've done the Apache configuration of Virtual Hosts:
<VirtualHost *:80>
ServerName www.new.com
DocumentRoot /www/old/html/new/
</VirtualHost>
I then proceeded to flush my local DNS cache. Yet still, it persists in changing the address bar in my browser to old.com/new. What am I missing? Does it just need time to propagate or have I misconfigured / failed to properly set something up?
You need to change the 1and1's "new.com" DNS entry to point to the same IP that "old.com" is using. While the htaccess rule (which I assume is at the new.com document root) kind of does what you want, it requires the mod_proxy be loaded, which is something I doubt 1and1 hosting allows.
What you need to do is set it up such that when you go to a site like this and do a DNS lookup for new.com, you get the same IP as when you lookup "old.com".
On old.com's server, you have the vhost setup:
<VirtualHost *:80>
ServerName www.new.com
DocumentRoot /www/old/html/new/
</VirtualHost>
which should be all you need to at least access the contents in /www/old/html/new/.

Capture referer in httpd access logs after a ReWriteRule

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!

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.

Forward a subdomain to a subfolder and preserve subfolder's .htaccess mod_rewrite rules

I have a standard LAMP setup and I'm trying to make subdomains automatically work as long as the appropriately-named folder exists.
If you're familiar with MediaTemple's GridServer shared hosting service, I'm trying to emulate the way it handles wildcard subdomains:
If subdomain.domain.com is requested, look for a folder named subdomain.domain.com
If the folder is found, call it home and serve up the site inside of it, obeying any .htaccess files that may be in there.
If no folder is found, just display domain.com
I've been told a .htaccess mod_rewrite in my root domain is the way to go, and I've been able to detect subdomains and at least point to the appropriate subfolder, but I don't think this leaves any opportunity for the subdomain.domain.com .htaccess file's own mod_rewrite to take over once the server knows where the subdomain's folder is located.
Here's my .htaccess for that:
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9-]+)\.domain.com$
RewriteRule ^(.+) /vhosts/%1.domain.com/html/pages/$1.php [L,QSA]
...And my folder structure:
domain.com
html
.htaccess
vhosts
subdomain.domain.com
html
.htaccess
pages
index.php
pagename.php
So as you can see, the site inside subdomain.domain.com is dependent on it's own mod_rewrite in order to function, since the page files aren't where the server expects them.
Now, I know I could probably include the subdomain's .htaccess rules in my root domain's .htaccess with the appropriate conditions, but the kicker is that I need to be able to point completely different domain names to these subdomains too (so the subdomain is accessible via subdomain.domain.com and mydomain.com), so these subdomain.domain.com folders need to be totally self-sufficient.
So how can I get my server to look in the correct location for a subdomain's folder, while allowing its own .htaccess mod_rewrites to work?
Any help is greatly appreciated!
Just had someone suggest that mod_rewrites are the wrong way to go about it, and mod_vhs is what I want to use. Does anyone know about mod_vhs?
I got it to work using VirtualHosts:
NameVirtualHost *
# Root domain homepage
<VirtualHost *>
DocumentRoot /var/www/domain.com/html
ServerName domain.com
ServerAlias www.domain.com
</VirtualHost>
# Hosted sites
<VirtualHost *>
VirtualDocumentRoot /var/www/vhosts/%0/html
ServerName *
ServerAlias *
</VirtualHost>
The first entry catches requests to my home page and serves up the standard site, while the second catches EVERYTHING else and routes to a different location based on the host name requested.
Easy. Just make sure the subdomain is registered with your server's DNS, then point it with an htaccess like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.domain\.com
RewriteRule .* /subfolder [L]
No need to mess with the subfolder's htaccess.