Forum Moderators: phranque

Message Too Old, No Replies

Internal Rewrite From Subdomain To Root

I checked the recommended search page.. I don't understand most of it

         

Zerxer

8:56 pm on Dec 19, 2007 (gmt 0)

10+ Year Member



Okay, I have been googling and searching this site and other sites for the past week now and I have tried many examples I've seen posted but I can't get any of them to work properly.

What I'm trying to do is internally rewrite (which I was calling 302 redirect for awhile) a request to a subdomain to the index in the root.

This is an example of what I'm trying to do:

User goes to sub.domain.com/folder_name and it rewrites to domain.com/index.php?page=folder_name (while still keeping the subdomain in the URL bar so it's an internal rewrite).

Along with help on making this code, I need to know a couple things:

Do I have to create the sub-domain via cPanel? I have looked around and people stating that you can set up "virtual subdomains" where if it's non-existant, it'll still act like it's there. I don't know if I can do virtual ones, or even know how to.. I'm doing this on a host account I'm paying for.

If I create the subdomain through cPanel, do I put the rewrite code in the .htaccess in the subdomain's folder or in my root?

One more thing that will decide whether or not I truly need this code: my site uses a lot of AJAX for certain things, and all the files are located in domain.com/files/scripts/ and I do not wish to copy them all over to a folder in the subdomain. Would trying to access these files from the subdomain (after it's internally rewritten anyways) using AJAX fail because the domains don't match up? Or would it be possible to try to grab the files via AJAX from the subdomain's folder and then have the .htaccess file rewrite these requests to the real files and still have the AJAX return correctly?

Thanks in advance.

Zerxer

6:21 pm on Dec 20, 2007 (gmt 0)

10+ Year Member



Hmm.. our Shared account got suspended for too much server load so we're going to be buying a Dedicated Server. Maybe that will help with what I'm trying to do?

jdMorgan

7:17 pm on Dec 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ajax, being client-side, has little or nothing to do with the question. The required procedure may vary, depending on how your cPanel is configured. If, having reviewed the previous posts, you have any specific questions, they are welcome. For information on how to get the most from this forum, please see our forum charter [webmasterworld.com].

While reviewing the previous threads, note that problems will be encountered if page-relative links are used on your pages to include images, JS, or CSS files; You will need to use server-relative or canonical links, or make provisions in the rewriterules themselves to resolve relative links to the proper filepaths.

Thanks,
Jim

Zerxer

3:50 pm on Dec 22, 2007 (gmt 0)

10+ Year Member



Yes, I did update all the relative links so that they were my own and all that. I at least knew that much. It's just hard trying to make something that was created for someone else's problem work for you when you don't know that much about what you're trying to do. I'm going to go check out those tutorials on that page you linked to and see if they can help me at all.

Also, my AJAX question did matter. I know that it's client-side. I asked what would happen if I put on a sub-domain page the is internally rewrote to the root an AJAX connection to a file that's in a folder on root, and not located on the sub-domain that is still in their URL bar. Won't it give me errors because they appear to be on different domains?

Then I asked, which is server-side, if I could use .htaccess to rewrite any connections to a file on the subdomain that I know AJAX was trying to grab over to the correct spot on root so that it would appear on the client-side that AJAX was actually grabbing a file on that subdomain so that it won't give access errors.

jdMorgan

6:27 pm on Dec 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



All that matters is URLs -- Ajax interfaces to the server just as would any other browser URL-request-mechanism such as clicking a link. That is why I said it is irrelevant. If clicking a URL as a text link works, then pulling the contents of that URL using Ajax will work equally well.

mod_rewrite maps URLs used on the Web to server filepaths. By default (without mod_rewrite), this mapping is essentially one-to-one (the server essentially removes the domain name and prepends the DocumentRoot path). But with mod_rewrite, the mapping becomes arbitrary -- You can map any URL that resolves to your server to any filepath within that server (at or below DocumentRoot). Keeping URLs and files as separate and distinct mental objects is quite helpful in forming a clear image of how all this works.

If you have access to the httpd.conf or conf.d files, as you should on a dedicated server, then I suggest you forget all about cPanel -- It is a generic script that can generate generic and often-very-sloppy code to insert into httpd.conf and .htaccess. In doing so, it often removes, corrupts, or bypasses custom code that you have inserted by hand into these files. So use caution with it -- Personally, I uninstall or disable "control panels" when possible.

Jim