2

I'm developing a project locally with NodeJS on www.foo.com and want to call a PHP app on subdomain.foo.com.

It works but it's interpreted as CROSS-ORIGIN so it sends OPTIONS requests and I can't get the thrown error from PHP in the Network browser development tool.

My hosts

127.0.0.1    foo.com
127.0.0.1    subdomain.foo.com

Apache

// foo.com
<VirtualHost *:80>
  ServerName foo.com
  DocumentRoot "/path/to/my/nodejs/app"
  <Location />
    ProxyPass http://127.0.0.1:8080/
    ProxyPassReverse http://127.0.0.1:8080/
  </Location>
</VirtualHost>

// Symfony2 project on: subdomain.foo.com
<VirtualHost *:80>
 ServerName subdomain.foo.com
 DocumentRoot "/path/to/my/php/app"
 <Directory /path/to/my/php/app>
   Options Indexes FollowSymLinks MultiViews
   AllowOverride None
   Order allow,deny
   allow from all
   <IfModule mod_rewrite.c>
       RewriteEngine On
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteRule ^(.*)$ /app_dev.php [QSA,L]
   </IfModule>
 </Directory>
  ErrorLog ${APACHE_LOG_DIR}/error.log
  LogLevel warn
  ErrorLog "/Applications/MAMP/logs/subdomain-foo-error.log"
  CustomLog "/Applications/MAMP/logs/subdomain-foo-access.log" common
</VirtualHost>

Any idea?

1 Answer 1

1

This is a cross-site XMLHttpRequests problem, so the solution is not really in PHP land, but in Apache itself.

You can do this by adding the line :

Header set Access-Control-Allow-Origin "*"

to the <Directory> section of the PHP site config. This will allow cross-site XHR requests from anywhere.

Or you can refine and say :

Header set Access-Control-Allow-Origin "foo.com"

if you only want requests from your main domain

Sign up to request clarification or add additional context in comments.

2 Comments

I understand, but I don't want to have a cross-site issue, I want to have both apps on the same domain. It works on my server but not locally. It seems that NodeJS stays on localhost:port and not foo.com!
They're on the same domain, but not the same subdomain. Cross-domain is exactly like cross-site. If this works on your server but not locally, then your server's Apache config is permissive and sets the Header for all virtual hosts, whereas your local configuration does not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.