- On some webservers there are more than one URL for a resource. Usually there are canonical URLs (which should be actually used and distributed) and those which are just shortcuts, internal ones, etc. Independent of which URL the user supplied with the request he should finally see the canonical one only. Add the following changes to the .htaccess file for the domain you wish to rewrite.
- Solution:
- We do an external HTTP redirect for all non-canonical URLs to fix them in the location view of the Browser and for all subsequent requests. In the example ruleset below we replace
/~user
by the canonical/u/user
and fix a missing trailing slash for/u/user
.RewriteEngine on
RewriteRule ^/~([^/]+)/?(.*) /u/$1/$2 [R] RewriteRule ^/([uge])/([^/]+)$ /$1/$2/ [R]
Canonical Hostnames
- Description:
- The goal of this rule is to force the use of a particular hostname, in preference to other hostnames which may be used to reach the same site. For example, if you wish to force the use of www.example.com instead of example.com, you might use a variant of the following recipe.
- Solution:
-
# For sites running on a port other than 80 RewriteEngine on RewriteCond %{HTTP_HOST} !^www\.example\.com [NC] RewriteCond %{HTTP_HOST} !^$ RewriteCond %{SERVER_PORT} !^80$ RewriteRule ^/(.*) http://www.example.com:%{SERVER_PORT}/$1 [L,R] # And for a site running on port 80 RewriteEngine on RewriteCond %{HTTP_HOST} !^www\.example\.com [NC] RewriteCond %{HTTP_HOST} !^$ RewriteRule ^/(.*) http://www.example.com/$1 [L,R]
Moved DocumentRoot
- Description:
- Usually the
DocumentRoot
of the webserver directly relates to the URL “/
“. But often this data is not really of top-level priority, it is perhaps just one entity of a lot of data pools. For instance at our Intranet sites there are/e/www/
(the homepage for WWW),/e/sww/
(the homepage for the Intranet) etc. Now because the data of theDocumentRoot
stays at/e/www/
we had to make sure that all inlined images and other stuff inside this data pool work for subsequent requests. - Solution:
- We redirect the URL
/
to/e/www/
:RewriteEngine on RewriteRule ^/$ /e/www/ [R]
Note that this can also be handled using the
RedirectMatch
directive:RedirectMatch ^/$ http://example.com/e/www/