Question

I am currently stuck with a problem in my local Drupal 7 environment. As Drupal 7 site configurations can get very complex, I will try to explain my problem in as much details as possible.

The site sits in a sub folder in my local environment, I have more projects running on my localhost, so preferably I would like to keep the projects separated. In addition, for this site I have two separate folders, one for development and one for production that share the same database, so a solution by adding fake domains would not work here I think (correct me if I'm wrong).

So the main problem seems to be that AJAX requests don't include the base URL or base path and I can't login on http://localhost/mysite/devel/docroot/user because the AJAX request would go to http://localhost/system/ajax or http://localhost/ajax_register/login/ajax and therefore would not return the correct JSON response.

How can this be solved? Are configurations in Apache's httpd.conf or .htaccess enough to make this work?

Here's what I did so far, first in settings.php:

$base_url = 'http://localhost/mysite/devel/docroot';
$base_path = '/mysite/devel/docroot/';

Next, I've tried the following with rewrite rules in httpd.conf:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} .*devel.*$ [OR]
    RewriteRule ^/sites/(.*)$ /mysite/devel/docroot/sites/$1
    RewriteRule ^/system/(.*)$ /mysite/devel/docroot/system/$1
    RewriteRule ^/ajax_register/(.*)$ /mysite/devel/docroot/ajax_register/$1
</IfModule>

Here I got the following pop up with HTML (that seems to be from index.php) in the response text instead of the expected JSON response:

An AJAX HTTP error occurred.
HTTP Result Code: 404
Debugging information follows.
Path: http://localhost/ajax_register/login/ajax
StatusText: error
ResponseText: lots of HTML...

Then without rewrite rules but using proxies instead in httpd.conf:

<IfModule mod_proxy.c>

    ProxyRequests Off
    ProxyPreserveHost On

    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>

    RewriteEngine on

    ProxyPass /system/ http://localhost/mysite/devel/docroot/system/
    ProxyPassReverse /system/ http://localhost/mysite/devel/docroot/system/
    <Location /system/>
        Order allow,deny
        Allow from all
    </Location>

    ProxyPass /ajax_register/ http://localhost/mysite/devel/docroot/ajax_register/
    ProxyPassReverse /ajax_register/ http://localhost/mysite/devel/docroot/ajax_register/
    <Location /ajax_register/>
        Order allow,deny
        Allow from all
    </Location>

</IfModule>

For the proxy directives, a similar 404 not found error was given for the POST AJAX request, except that now the response text is in JSON format.

An AJAX HTTP error occurred.
HTTP Result Code: 404
Debugging information follows.
Path: http://localhost/ajax_register/login/ajax
StatusText: error
ResponseText: some JSON code...

Without both the rewrite rules and the proxy directives I get the following error in the JavaScript pop up:

An AJAX HTTP error occurred.
HTTP Result Code: 404
Debugging information follows.
Path: http://localhost/ajax_register/login/ajax
StatusText: error
ResponseText: 
404 Not Found
Not Found
The requested URL /ajax_register/login/ajax was not found on this server.

Finally in .htaccess I've tried to rewrite the base to the following:

RewriteBase /mysite/devel/docroot/

and here I get the same 404 error as was the case when both the rewrite rules and proxy directives are commented out in httpd.conf. I would also like to add that in the database, in the table languages I've set the domain for the English language to localhost.

I don't understand, why is the base not included in front of the AJAX URL requests? And how can I add it? When I query Drupal.settings.basePath in Firebug I do get the value that I've set in settings.php :S - Does someone have any ideas?

Was it helpful?

Solution

There are many solutions for your problem, but I'm gonna try to focus on the most mature one. I assume you are using windows as your environment.

You said you were running your projects on localhost, which is where your first mistake is.

Localhost is nothing but a form of weird domain name you specify. When you request a domain like "www.google.com" or "localhost" the following steps occur:

  1. The browser checks a specific file for the requested domain. This file is called hosts file
  2. If the domain name is found in the hosts file, the browser sends a request to the IP address specified in the hosts file
  3. If the domain name is not found in the hosts file, the browser queries domain name servers which return the corresponding IP adress.

Now localhost is nothing but a domain name specified in your hosts file, which points to the loopback address.(127.0.0.1).

So the magic trick here is to bind your custom hosts like "project1", "project2" etc.. to that loopback adress(127.0.0.1).

Than when you send a request to "project1" in your browser, the server running on port 80 will respond as if you typed "localhost".

The second part you need to take care of is called virtual hosts. When you send a request with a specific domain name, a special header is included in your http request called "Host".

Lets assume, that you redirect all this custom domains to the same IP (127.0.0.1). In order for apache to serve a different project, you should instruct apache to look at the "Host" header and resolve it for the corresponding project.

Again you do that by setting virtual hosts.

A lot of frameworks and content managing systems in PHP have some ugly ways to insert some magic "$BASE_PATH" variable, which is a bad practice as that could be achieved with relative paths in pure html and a properly configured server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top