Forum Moderators: phranque

Message Too Old, No Replies

rewrite rule to enforce FQDN

         

fletch00

6:51 pm on Jan 25, 2010 (gmt 0)

10+ Year Member



Hi,
I am looking for a rule to enforce fully qualified domain names.
Since we have hundreds of sites under xyz.edu we want all requests for [ABC...] to be rewritten to [ABC.xyz.edu....]
Where ABC is wildcarded in the rule since it can be any one of the hundreds of sites that resolve to our VirtualDocumentRoot configured apache.

Many thanks for any tips/pointers!

Fletch.

g1smd

8:51 pm on Jan 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



To be clear, you want example.com to redirect to example.com.somwhere.edu where 'example' could be anything?

More detail is needed. Does 'example' contain hyphens or other punctuation.

Ah, you're talking 'rewrite' not redirect, so what is the internal server path that such a request will resolve to, and what URL request will match that?

fletch00

5:17 pm on Jan 26, 2010 (gmt 0)

10+ Year Member



Yes exactly
thanks

jdMorgan

6:06 pm on Jan 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It won't be possible to correctly answer this question without a comprehensive, non-redundant list of examples of all allowed requested-URL variations and the paths to which these should be resolved.

Protocol (HTTP/HTTPS), subdomain, domain, FQDN/non-FQDN indicator (trailing period on domain), port number, URL-path, query, and fragment identifiers may all vary, and if so, these variations must be accounted for. While in some cases, it's easy to say "all," this is usually not the case, and further, this can complicate efforts to prevent 'infinite' redirection loops.

For example, a requested URL could well be "http://www.subdomain.example.com.:80/url-path1.html?sid=1234#named-anchor", or it might be "https://example.com/user-1234/"

Redirection and rewriting generally require the use of regular-expressions patterns to "decide" whether and what to redirect/rewrite. The allowed variations in the requested URL must be taken into account when writing these patterns. So a list of URLs demonstrating the nature and range of variations is most useful.

Otherwise, you'll have requests which don't get redirected/rewritten as desired and/or requests which do get rewritten/redirected when this is not desired.

The basic plan is to check the request variable HTTP_HOST and if non-blank, extract the hostname and inject it into the URL-path. However, there are likely some exceptions such as "www", where it's unlikely that you want to treat this subdomain as a "username."

Also, to prevent future performance and functional problems, it's a good idea to rewrite all "user-subdomain" requests to a separate subdirectory, so that collisions between usernames and directories required for site infrastructure cannot occur. For example, rewrite <username>.example.com/foo.html to /users/<username>/foo.html instead of rewriting it to /<username>/foo.html

A basic http-only example using mod_rewrite in a server config file, outside of any <Directory> containers, would be:


# Externally redirect to remove leading "www" from subdomain requests and force canonical
# domain (add "www" if missing, remove FQDN indicator and port number if present)
RewriteCond www.%{HTTP_HOST} ^(www)\.(www\.)?example\.com\.(:[0-9])?$ [OR]
RewriteCond www.%{HTTP_HOST} ^(www)\.(www\.)?example\.com:[0-9]$ [OR]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com
RewriteRule ^/(.*)$ http://%1.example.com/$1 [R=301,L]
#
# Internally rewrite all "username" subdomain requests except "www" to
# "users/<username>" subdirectory
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^/(.*)$ /users/%1/$1 [L]

If further processing by URL translation modules (e.g. mod_alias or mod_proxy) will be required, add the [PT] to the second rule's flags.

If this code is to be deployed in a .htaccess file, then loop-prevention will be required. Add a rewritecond to the second rule:


RewriteCond %{REQUEST_URI} !^/users/

That will work if the recommended "/users/" subdirectory approach is used. If not, then it will be necessary to check %{REDIRECT_STATUS} for blank instead.

[added] Also, if this code is used in .htaccess or in a server config file within a <Directory> container, the leading slash must be removed from both rules' patterns. [/added]

Jim