0

I am administrating an Apache/2.2.15 webserver with several virtual hosts. For one of these hosts I want to always prepend a missing www.. Besides that, I want to add several short-cut URLs. Here is what I have done so far:

RewriteEngine On
Redirect /short /some-long-URL
RewriteCond %{HTTP_HOST} ^my-domain\.org$ [NC]
RewriteRule ^.*$ http://www.%{SERVER_NAME}$1 [R=301]

With these rules, accessing an URL of the form http://my-domain.org/some-URL is rewritten and redirected to http://www.my-domain.org/some-URL, which is exactly what I want. However, when accessing http://my-domain.org/short, I get redirected to http://www.my-domain.org/. It seems like the Redirect rule for /short is never being applied... But accessing http://www.my-domain.org/short is correctly being redirected to http://www.my-domain.org/some-long-URL.

I hope someone knows a solution and can explain to me, what I am doing wrong.

Regards, Immanuel

PS: I would also appreciate it if the solution would also work with RewriteRule with PassThrough instead of a redirect :)

  • `With these rules, accessing an URL of the form` - The description you give for what's happening doesn't match the rules in the question (you can counter/disprove this comment by adding the output of `curl -I http://my-domain.org/some-URL` etc. to the question). If you want things to work with PassThrough - start by not redirecting /short to /long, and use a rewrite rule for it _with passthrough_ =). – AD7six Nov 30 '14 at 10:13

2 Answers2

0

I would define the virtual host to have the desired domain as its server name.

# Virtual host definition with desire canonical name
ServerName      www.example.com
ServerAlias     example.com
Use CanonicalName on

You could use a passthrough rule to avoid the redirect. This is invisible to the client (browser). There are multiple ways to redirect the request. I've shown two.

RewriteEngine On
RewriteBase /

# Handle rewrite without redirect (passthrough) 
RewriteRule /short /some-long-URL [PT]

# Or use a redirect (scheme and server not required as of 2.2.6)
Redirect /short http://%{SERVER_NAME}/some-long-URL

# Or use rewrite and redirect  
Rewrite ^/short some-long-URL [R]

If you use canonical names, you can just redirect non-matching hosts.

RewriteCond %{HTTP_HOST} !^%{SERVER_NAME}$ [NC]
RewriteRule ^(.*)$ http://%{SERVER_NAME}/$1 [L,R=301]

I generally use hostnames instead of using %{SERVER_NAME}. If you use the same configuration for both HTTP and HTTPS, you may want to use %{REQUEST_SCHEME} instead of http in the URLs

BillThor
  • 27,737
  • 3
  • 37
  • 69
0

Rewrite changing domain

A drop in rewrite rule to rewrite example.com → www.example.com is:

 RewriteCond %{HTTP_HOST} !^www\. [NC]
 RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

As mentioned by BillThor, http can be replaced with %{REQUEST_SCHEME} to be scheme independent.

What's wrong with the rules in the question?

I hope someone knows a solution and can explain to me, what I am doing wrong.

This rewrite rule:

RewriteRule ^.*$ http://www.%{SERVER_NAME}$1 [R=301]

Has no capture group, hence $1 is an empty string.

AD7six
  • 2,920
  • 2
  • 21
  • 23