skip to Main Content

I have a laravel project that has a main domain and a subdomain setup in routes/web.php like this:

Route::group(array('domain' => '{account}.example.com'), function() {
    // subdomain routes
});

Route::group(array('domain' => 'example.com'), function() {
    // domain routes
});

how can i separate robots.txt files for main domain and subdomain? if you going to the path of robots.txt, domain and subdomain return the same file. Laravel read default file in public folder.

3

Answers


  1. I think you can do it this that are in Laravel Documentation

    Route::domain('{account}.example.com')->group(function () {
        Route::get('user/{id}', function ($account, $id) {
            //
        });
    });
    
    Login or Signup to reply.
  2. OK. Solution for NGINX.

    Before server section:

    map $host $robots_file {
      default robots.txt;
      site.com robots-site.txt;
      www.site.com robots-site.txt;
    }
    

    Inside server section:

    location = /robots.txt {
      try_files /$robots_file =404;
    }
    
    Login or Signup to reply.
  3. One solution that you can use on Apache servers is to create a custom rule that routes robots.txt depending on the subdomain.

    You can have in the public folder, two robots files, for example robots.txt and robots-disallow.txt.

    robots.txt:

    User-agent: *
    Disallow:
    

    robots-disallow.txt:

    User-agent: *
    Disallow: /
    

    In the public/.htaccess file, create a new rule which will indicate that if the request is to one of the indicated subdomains, the path to robots.txt will show the content of robots-disallow.txt, otherwise it will show the original content of robots.txt file.

    .htaccess:

    <IfModule mod_rewrite.c>
         RewriteEngine on
         RewriteCond %{HTTP_HOST} ^sub1|sub2.domain.com$
         RewriteRule ^robots.txt$ robots-disallow.txt
    </IfModule>
    

    In this way, when sub1.domain.com is accessed, the sitemap will be displayed indicating that the content should not be indexed, and in the main domain the general robots.txt.

    This solution works not only in Laravel but also in any apache environment.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search