Apache Configuration: redirecting to www in .htaccess - apache

I have read a number of formulas for redirecting example.com to www.example.com and this appears to apply to .htaccess. However, I am confused about how this might work.
I want to assume that I have no access to the Apache vhosts configuration, and that I need to do it with .htaccess.
Suppose the configuration contains something like this:
<VirtualHost *:80>
ServerName www.example.com:80
ServerAlias www.example.com
VirtualDocumentRoot /whatever/example.com/www
</VirtualHost>
One such formula is something like this"
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ www\.%{HTTP_HOST}/$1 [R=301,L]
However, I don’t see how one root directory can respond to both requests.
The question is: Is it even possible to redirect example.com to www.example.com using .htaccess only, without tweaking the Virtual Host file?
The way I see it, the original request
I run my own server, and I can do anything I like. However, I am asking this because many of my clients and students have no access to tehe configuration, and can only fiddle with .htaccess.

Related

How to skip HTTPS redirection, only if the url contains a substring?

In my Apache http.conf file, I have the following redirect setup, which means that if I access an endpoint using my server's domain name, it serves its HTTPS equivalent, instead of HTTP. However there's a particular URL where I do not want this address to occur. This is how i've got it set up at the moment:
<VirtualHost *:80>
ServerAdmin me#mydomain.com
DocumentRoot "/srv/httpd"
ServerName example.com
ServerAlias example.com
RewriteEngine On
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
So what do I need to add, so that if I were to access http://example.com/insecure/ (and anything within that folder), it doesn't redirect me anywhere, however it does redirect to HTTPS if i'm accessing anything else on my domain? Just a second RewriteCond? I'm not sure of the syntax
I've added a second RewriteCond of RewriteCond %{REQUEST_URI} !/insecure/ which seems to have done the job.

Apache2 - Using a lowercase subdomain to serve an uppercase folder

Currently the website being hosted uses the following structure of test.mysite.com/{site} which then serves /var/www/test/{site}
I've been trying to create a virtualhost that can match several subdomains and serve the folders under /var/www/test/. The urls would look like {site}.test.mysite.com
I've tried several approaches, one using virtual document root:
<VirtualHost *:80>
ServerName test.mysite.com
ServerAlias *.test.mysite.com
VirtualDocumentRoot /var/www/test/%1/
</VirtualHost>
This works well except that the folders under /var/www/test/ are in uppercase and I cant seem to get the %1 to uppercase.
A different approach was using mod rewrite as specified on the apache site
<VirtualHost *:80>
ServerName test.mysite.com
ServerAlias *.test.mysite.com
RewriteEngine on
RewriteMap upper int:toupper
RewriteMap lowercase int:tolower
RewriteCond ${lowercase:%{SERVER_NAME}} ^[a-z0-9-]+\.test\.mysite\.com$
RewriteRule ^([a-z0-9]+)\.test\.mysite\.com/$ /var/www/test/${upper:$1}/
</VirtualHost>
However it never seems to actually rewrite and just keeps serving the default apache document root.
I'd prefer not to have to change the name of all the folders to lowercase due to other dependencies. Are there any other options for doing this?
Your rule is wrong: RewriteRule, here, only apply to the full path of the URL which does not include the host. Try:
RewriteCond %{HTTP_HOST} ^([a-z0-9-]+)\.test\.mysite\.com$
RewriteRule .* /var/www/test/${upper:%1}$0 [L]

How to redirect all/any page request on a site to a certain static page on same site

I need every page request on a site -- 200, 404, etc. -- to land at a fixed page on the site, in this case index_new.php.
I've tried other SO and ServerFault answers and I continue to get an endless redirect loop.
Here's my code in httpd.conf:
<VirtualHost *:80>
ServerName my.example.com
ServerAlias *.my.example.com
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index_new.php
RewriteRule ^(.*)$ /index_new.php [R=301,L]
</VirtualHost>
Notes:
the site's .htaccess file is empty
the "index_new.php" page is static with no PHP header("location:") internal redirects, etc.
I have restarted apache
A Negative lookahead should do, and can easily be done with a single line with RedirectMatch instead of using mod_rewrite:
RedirectMatch permanent ^/(?!index_new.php).* /index_new.php
Clear your browsers cache and/or try with curl -I http://my.example.com/wahtever to make sure it is working
For sanity specify documentroot in the virtualhost and a directory entry to allow access (Require all granted / Allow from all depending on the version) and since you are not using .htaccess (btw congratulations! you are one of the few doing things right from the start), set the directory of the DocumentRoot with "AllowOverride none" just to be safe.

How do I redirect a user using Apache Rewrite, to the fully qualified domain name?

I'm really new to apache mod_rewrite module. I have a page called http://abc in my company intranet. I want users to be redirected to http://abc.somecompanyname.com whenever they type http://abc to the URL bar. Could someone please provide and example or point me in the right direction.
I figure this should be quite an easy question to answer. Thanks everyone for you inputs.
-Mark
Quote from Apache 2.4 documentation:
The very best way to solve this doesn't involve mod_rewrite at all, but rather uses the Redirect directive placed in a virtual host for the non-canonical hostname(s).
<VirtualHost *:80>
ServerName undesired.example.com
ServerAlias example.com notthis.example.com
Redirect / http://www.example.com/
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
</VirtualHost>
This does require another virtual host, but there's no shortage of those. The solution works very well for me - and I like how redirection of 'unwanted' hosts and configuration of the canonical host are separated.
You could accomplish that with a VirtualHost definition as simple as this, on the server handling requests for abc:
<VirtualHost *:80>
ServerName abc
RewriteEngine on
RewriteRule ^/(.*)$ http://abc.somecompanyname.com/$1 [R,L]
</VirtualHost>
I found the advise in the Apache2 URL Rewriting Guide worked better.
I ended up with:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^foo\.bar\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://foo.bar.com/$1 [L,R]
The "RewriteEngine on" line wasn't included in the Apache2 example. Maybe it's usually on by default but in my case I needed to add it.

Redirect/rewrite alternative domains to root domain with apache2/htaccess

I 2 domains with and without the www prefix. When a user visits any of these domains, I want it to automatically reroute to a chosen 1 of them.
For example:
domain.com
www.domain.com
domain.co.uk
www.domain.co.uk
When a user visits www.domain.com, domain.co.uk or www.domain.co.uk, it will rewrite to domain.com
So far, I have my apache2 virtual host block setup like this:
<VirtualHost *:80>
ProxyPass / http://localhost:3060/
ProxyPassReverse / http://localhost:3060/
ServerName domain.com
ServerAlias www.domain.com
ServerAlias domain.co.uk
ServerAlias www.domain.co.uk
</VirtualHost>
But this doesn't do the rewriting/rerouting. I need to also make sure that it takes into account any paths. For example, www.domain.co.uk/test would change to domain.com/test
Any ideas how I can do this in the virtual host block? I'm assuming I would split the 3 domains to be rewritten into a separate block and treat them there, but really not sure how to accomplish all the rules I need.
Per your comment, you want to redirect the three aliases to the main domain, and you've stated that you would like to do this within the virtual host configuration.
<VirtualHost *:80>
...
RewriteEngine on
# If using one of the aliases ...
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.uk
# ... redirect to the main domain
RewriteRule ^(.*)$ http://domain.com/$1 [R=302,L]
</VirtualHost>
You can also add the Rewrite* directives in your domain's .htaccess file.
To make the redirect permanent, change 302 to 301 - this basically instructs browsers and search engines to cache the redirect.