I'm attempting to create a unique series of redirects on an Apache web server (Server version: Apache/2.2.15). Using .htaccess or additional Apache modules, I need to fully parse requested URLs in order to extract hostnames without the standard .com suffix.

Example: I need vendorname from the hostname vendorname.com.

The business I'm supporting works with third-party vendors and has created unique top-level domains for each of them that points to a vendor-specific sub-directory in the document root of the web server.

Example: vendorname.com/logo.jpg loads an asset that can also be accessed at parentbusinessdomain.com/vendorname/logo.jpg

This convention was put into place by the business 10 or 15 years ago and there are many, many vendors. What the business didn't account for was the need to support HTTPS/SSL implementation for each unique domain. Had they gone with sub-domains such as vendorname.parentbusinessdomain.com then this would have been a non-issue. Going forward, we wish to abandon this convention and simply host images and other assets from https://parentbusinessdomain.com.

The problem I'm continuously running into is that there doesn't seem to be a standard Apache string operation or server-side variable which represents or extracts the hostname without the specific string ".com". This sort of string extraction is feasible with backend PHP or frontend Javascript but it appears that I'm limited with what .htaccess itself can do.

Similar example with custom Javascript function: https://stackoverflow.com/a/16934798/602514



Object {
domain : "www.facebook.com",
host : "facebook",
path : "100003379429021_356001651189146",
protocol : "https",
subdomain : "www",
tld : "com"

The contingency will be to explore URL rewrite/redirects within the legacy web application itself but hoping there may be a more elegant solution from an infrastructure perspective.

  • 1
    Are all the domains single TLD like your examples .com etc.? Do you have any STLD like .co.uk etc.?
    – MrWhite
    Oct 5 at 21:14
  • 1
    What is the URL you are actually linking to?
    – MrWhite
    Oct 5 at 21:32
  • @MrWhite these vendor domains all .com, didn't mean to complicate things by suggesting they were varied domain types. As far as destination domain, if we could just say target domain is example.com that would preserve my client's anonymity. Thanks in advance!
    – sparecycle
    Oct 5 at 22:29
  • Aside: "This sort of string extraction is feasible with backend PHP" - You'd actually do this pretty much the same way in any language (PHP, JS or Apache); by examining the Host header. Apache has all the tools. You wouldn't use a JS function like that if you are examining the current request.
    – MrWhite
    Oct 6 at 1:26

You can do something like the following at the top of your .htaccess file using mod_rewrite (although this will work just as well directly in the server config):

RewriteEngine On

RewriteCond %{HTTP_HOST} !^example\.com
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]+) [NC]
RewriteRule ^ https://example.com/%1%{REQUEST_URI} [R=302,L]

This would redirect <vendorname>.com/<anything> to https://example.com/<vendorname>/<anything>. Where <anything> can literally be any URL-path. Any query string is also preserved across the redirect.

example.com is the "parentbusinessdomain". The first condition simply checks that the request is not already for the target domain.

The %1 backreference matches the "vendorname", captured from the requested hostname vendorname.com in the preceding CondPattern. This allows for an optional www subdomain. You mentioned that you have just .com domains, although this would work with any (multi-level) TLD, just assuming there are no other subdomains (except for an optional www).

Test first with 302 (temporary) redirects and only change to a 301 (permanent) - if that is the intention - once you have confirmed it works as intended.

What the business didn't account for was the need to support HTTPS/SSL implementation for each unique domain.

Just curious how the redirect resolves this? Is the redirect just to preserve SEO and any 3rd party backlinks that are to URLs of the form http://vendorname.com/logo.jpg (HTTP only)? (Although you wouldn't normally expect backlinks to logo.jpg and this particular example doesn't really do much to help SEO?)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.