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
Maybe this is the right way to do in laravel:
You can change the root directory in your controller or route by using
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
Without touching laravel code, but with nginx config:
Folder structure:
index.php:
result calling:
http://www.test.test/2016/sample/foo/bar
…
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)