Forum Moderators: phranque
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.
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
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.
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