Eleonora Dirk
Eleonora Dirk

Reputation: 1

Laravel applicaton with dynamic subdomains, but wordpress page on main domain?

I have dynamic laravel application, after registration user get it's own subdomain example.myapp.com and that's fine, now the agency develops marketing website which we would like to place on main domain: myapp.com.

What I tried:

  1. I tried to play around with A record but that doesn't worked.

  2. The possibility would be of course to load everything with curl but then that's not SEO friendly because the page would need to be hosted on a subdomain as well.

Upvotes: 0

Views: 71

Answers (1)

I would solve this issue by using a reverse proxy (NGINX for example).

Note that this is only a pseudo example!

# Server block for subdomains
server {
    listen 80;
    server_name *.example.com;

    location / {
        proxy_pass http://subdomain_backend_server;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

# Server block for URLs without subdomain
server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://no_subdomain_backend_server;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

In the above configuration:

  • The first server block uses the wildcard *.example.com in the server_name directive to match all subdomains of example.com. Requests to these subdomains will be proxied to http://subdomain_backend_server.
  • The second server block has the server_name directive set to example.com, matching requests without a subdomain. These requests will be proxied to http://no_subdomain_backend_server.
  • The location / block in both server blocks specifies the URL path to match for proxying. In this case, it proxies all paths. The proxy_set_header directives are used to pass along the necessary headers to the respective backend servers.

Make sure to replace example.com with your actual domain name, subdomain_backend_server with the URL of the backend server for subdomains, and no_subdomain_backend_server with the URL of the backend server for URLs without a subdomain. Adjust the configuration as needed for SSL/TLS or any other specific requirements.

Hope this helps you on the way!

All the best.

Upvotes: -1

Related Questions