Forum Moderators: phranque

Message Too Old, No Replies

Rewrite from one subdomain to another without redirection

         

cschults

3:17 am on Aug 28, 2007 (gmt 0)

10+ Year Member



I have two virtual sites on the same server. Each has its own apache conf file.

I need www.example.com/foo/ to rewrite to subdomain.example.com/section/foo without browser redirection.

Is this possible? If so, any suggestions?

cschults

3:48 am on Aug 28, 2007 (gmt 0)

10+ Year Member



As I continue to research this, using a reverse proxy via mod_proxy [httpd.apache.org] looks promising, but it appears there might be some drawbacks using mod_proxy. Any thoughts?

jdMorgan

1:03 pm on Aug 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It would be helpful to the discussion if you were to state why you find it necessary to do this, and what level of control you have over the server, i.e. can you edit httpd.conf?

One solution is to declare all subdomains as ServerAliases of the main domain or point them all to the same DocumentRoot, and then use mod_rewrite to 'sort them all out' into their own individual filespaces as needed, or into a common filespace as required. This can be accomplished by using RewriteCond to test the HTTP_HOST variable to get the requested hostname and act upon its value.

But it's impossible to recommend a solution, not knowing the actual problem.

Jim

cschults

3:00 pm on Aug 28, 2007 (gmt 0)

10+ Year Member



Sure Jim, I wasn't sure if specificity was required in this case.

We're launching a new News section on our website managed by one CMS that outputs static HTML pages.

This News section will be published to www.example.org/news/ and a sample story URI might be /news/2007/08/28/sample/index.html.

We also need a paginated series of index pages that list the last 20 stories published to the News section. While the same CMS could produce these index pages, over time they would add up in the hundreds (or possibly thousands) since we'll be publishing a number of news stories each day. While this isn't necessarily a problem per se, we have an another option.

We have a blog at gristmill.example.org, which is powered by a second CMS. This is a dynamic site and we use it to power commenting on the statically generated website. We do this by auto-publishing dummy stories to the second CMS and then embedding the comment threads into the static HTML pages. Because we're doing this already, the second CMS is already generating an index page at gristmill.example.org/section/news.

But the desire is to have the URL of the News index page to be www.example.org/news/ without browser redirection.

And yes, I can edit our apache conf files.

[edited by: jdMorgan at 3:10 pm (utc) on Aug. 28, 2007]
[edit reason] No URLs, please. See Terms of service. [/edit]

jdMorgan

3:38 pm on Aug 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are many tools at your disposable, all with possible side-effects. Not being familiar with your particular CMS installs, all I can do is point them out, and you can work your way through them with some simple testing.

From lowest level to highest, here are the ones that come to mind:

  • Symbolic links in the filesystem (might not work due to 'permissions' problems)
  • Internal rewrites using mod_rewrite if the DocumentRoots are common between domains
  • Alias using mod_alias
  • mod_proxy -- Pass the requests through the example.org server to the gristmill.example.org server. This has some user-tracking and performance implications, though, because all proxied requests for the news-index pages on the gristmill.example.org server will appear to be coming from the example.org server, not from your visitors.
  • Combine the subdomain with the main domain, since they are evidently not completely-independent sites. This would ease 'resource sharing' complications between them, as in this case.

    Be on the lookout for problems with file permissions, cookies, sessions, user authorization, and SSL while working on this.

    Jim

  • cschults

    5:35 pm on Aug 28, 2007 (gmt 0)

    10+ Year Member



    Thanks again Jim. The problem is that the index page is dynamically generated from info stored in a database -- so there is no actual file for which to create an alias or symbolic link. (I'm working under the assumption you need an actual file for the alias directive and symbolic links -- please correct me if I'm wrong.)

    Also, while the long term plan is to have one website (rather than a website and a blog) managed by one CMS, combining the subdomain and domain is not an option at this time.

    Thus, it looks like the only viable option (of the ones discussed thus far) is mod_proxy, but I'm not crazy about the tracking and performance issues that you mentioned.

    I suppose I could employ the same technique that I'm using to allow commenting on static pages, which involves embedding some Perl in the HTML that gets and prints content using LWP::UserAgent.

    jdMorgan

    6:24 pm on Aug 28, 2007 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    When reading the Apache documentation, pay careful attention to the terms "URL" and "file." These are two different objects, one used on the Web, and the other used in the filesystem. As demonstrated by directives like "DocumentRoot" and by modules like mod_rewrite and mod_alias, they are not the same thing, and indeed, need not have any sematic relationship -- The fundamental job of a server is to translate a URL to a filepath.

    Alias directive

    The Alias directive allows documents to be stored in the local filesystem other than under the DocumentRoot. URLs with a (%-decoded) path beginning with url-path will be mapped to local files beginning with directory-filename.

    I believe you may find the Alias [httpd.apache.org] directive entirely adequate for your application, as long as you don't have any of the complicating issues with permissions, cookies, etc. previously mentioned. Alternatively, the AliasMatch directive offers the more sophisticated regular-expressions matching of requested URL-paths, rather than the simple prefix-matching used by Alias.

    Jim

    cschults

    6:59 pm on Aug 28, 2007 (gmt 0)

    10+ Year Member



    As I read the mod_alias documentation, you can map a URL-path to a file or directory path, but it doesn't appear possible to map a URL-path to another URL-path, which I believe I'm trying to do.

    Again, the index page (gristmill.example.org/section/news) is dynamically generated. And I just realized it is actually gristmill.example.org/?op=section&section=news. The CMS obviously employs its own rewriting.

    So, I added the following to my conf file (for www.example.org):

    AliasMatch /news$ http://gristmill.example.org/section/news

    But upon testing resulted in a 404 with the following message in the error log:

    File does not exist: http://gristmill.example.org/section/news

    I expected this as there is no file to map to.

    What am I missing?

    Thanks again for all your help.

    [edited by: jdMorgan at 7:09 pm (utc) on Aug. 29, 2007]
    [edit reason] example.org. [/edit]

    cschults

    6:28 pm on Aug 29, 2007 (gmt 0)

    10+ Year Member



    I've decided to use an include, but any additional thoughts would be welcome as I would be interested in knowing if this is possible using mod_alias as that seems more elegant.

    jdMorgan

    7:24 pm on Aug 29, 2007 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    If gristmill.example.org and example.org are on the same server, then mod_alias can be used to connect the URL gristmill.example.org/section/news or gristmill.example.org/?op=section&section=news to the file /htppd/grist/www/?op=section&section=news

    It matters not one bit that this file is a CMS script. It lives in the server filesystem, and should be accessible there. Your main challenge is to find it, because in my example, "/httpd/grist/www/" is a made-up path. The path to the script should be easy to construct, as it will be based on the path declared as DocumentRoot for the 'main' domain.

    Jim

    g1smd

    7:52 pm on Aug 29, 2007 (gmt 0)

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



    The key point that I missed for a long time was that URLs and filepaths are different things, and that they do not have to have a direct one-to-one relationship. "Translation" can be done.

    Once that penny dropped, it became very easy to have a URL system that was search engine friendly, and was structured completely different to the file structure on the server itself.