skip to Main Content

Situation: webproject where this is the folder structure.

  • www
    • 2015
    • 2016
    • 2017
    • 2018
    • current

Each folder represents the website of that year, in the “current” folder it is the website that should be shown when you go to www.mydomain.com.
The goal is that if you go to www.mydomain.com/2017 you see the website of 2017.

There is a .htaccess file in the root folder that makes sure that if you navigate to the “/” you will be redirected to the current one.

I am struggling with the .htaccess file I have to put in the “2017” folder (which is a laravel application).

.htaccess file in www folder

<IfModule mod_rewrite.c>
   Options +SymLinksIfOwnerMatch
   RewriteEngine on

   RewriteRule ^(.*)$ current/$1 [L]
</IfModule>

.htaccess file in 2017 folder

<IfModule mod_rewrite.c>
   Options +SymLinksIfOwnerMatch
   RewriteBase /2017
   RewriteEngine on
   RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

Current result: when I go to www.mydomain.com/2017, I go to the 2017 folder, but it goes to page “2017” of 2017. To test this I just added this in my web.php route:

Route::get('/{page}', array('as'=>'renderpage',function ($page) {
    dd('I am here and the page you request is '.$page);
}));

which results in:
“I am here and the page you request is 2017”

So long story short: how can I remove the “2017” from the url in the .htaccess file, so that laravel interprets www.mydomain.com/2017 as the root of the 2017 folder. Preferably this should be done in the .htaccess file in the 2017 folder.

edit:
I changed the .htaccess file in the 2017 folder to this:

<IfModule mod_rewrite.c>
   Options +SymLinksIfOwnerMatch
   RewriteBase /2017
   RewriteEngine on
   RewriteRule ^(/.*|)$ public/$1 [L,NC,R=301]
</IfModule>

now it works, except in the url you now see www.mydomain.com/2017/public
how can I remove the “public” from the url?

2

Answers


  1. Maybe this is the right way to do in laravel:

    You can change the root directory in your controller or route by using

    $this->app->bind('path.public', function() {
      return base_path().'/public_html';
    });
    

    you could insert an absolute path in there i guess.

    Check out

    https://laravel.com/docs/5.7/helpers#method-public-path

    https://laravel.com/docs/5.7/helpers#method-base-path

    Login or Signup to reply.
  2. Without touching laravel code, but with nginx config:

    server {
        listen *:8080;
        server_name 2016.test.test;
    
        root /var/www/html/2016;
        index index.html index.php;
    
        location / {
            try_files $uri $uri/ /index.php$is_args$args;
        }
    
        location ~ .php$ {
            fastcgi_index   index.php;
            include fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
        }
    } 
    
     server {
        listen                *:80;
    
        server_name           www.test.test;
        client_max_body_size 1m;
    
        root /var/www/html/current;
        index  index.html index.htm;
    
        location /2016/ {
            rewrite /2016/(.*) /$1 break;
    
            proxy_pass          http://2016.test.test:8080;
            proxy_redirect      off;
            proxy_set_header    Host $host;
        }
    }
    

    Folder structure:

    .
    ├── 2015            
    │   └── index.html  
    ├── 2016            
    │   ├── index.html  
    │   └── index.php   
    ├── 2017            
    │   └── index.html  
    ├── current         
    │   ├── index.html  
    │   └── test.html   
    └── index.html
    

    index.php:

     <?php
         var_dump($_SERVER);
     ?> 
    

    result calling:
    http://www.test.test/2016/sample/foo/bar

      ...
      'SCRIPT_FILENAME' => string '/var/www/html/2016/index.php' (length=28)
      'REDIRECT_STATUS' => string '200' (length=3)
      'SERVER_NAME' => string '2016.test.test' (length=14)
      'SERVER_PORT' => string '8080' (length=4)
      'SERVER_ADDR' => string '127.0.0.1' (length=9)
      'REMOTE_PORT' => string '48598' (length=5)
      'REMOTE_ADDR' => string '127.0.0.1' (length=9)
      'SERVER_SOFTWARE' => string 'nginx/1.14.0' (length=12)
      'GATEWAY_INTERFACE' => string 'CGI/1.1' (length=7)
      'REQUEST_SCHEME' => string 'http' (length=4)
      'SERVER_PROTOCOL' => string 'HTTP/1.0' (length=8)
      'DOCUMENT_ROOT' => string '/var/www/html/2016' (length=18)
      'DOCUMENT_URI' => string '/index.php' (length=10)
      'REQUEST_URI' => string '/sample/foo/bar' (length=15)
      'SCRIPT_NAME' => string '/index.php' (length=10)
      ...
    

    you see REQUEST_URI is just ‘/sample/foo/bar’ without your year

    This example is hardcoded to 2016, you can easily just use some regex for location in year like: 20[0-9]{2} and replace match with the proper variable ($1 or something like this)

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