I'm slowly migrating my website into the artisan era using Laravel.
I have it setup on AWS
on a vanilla Amazon linux.
Currently I have;
www.example.co.uk
blog.example.co.uk
careers.example.co.uk
These are all on the same server and the subdomains have been achieved using htaccess
to redirect to \blog
and \careers
folders in my root
with DNS
being handled by Route 53
Now in moving to laravel
it only considers the public
folder (htaccess
use no longer required apart from the default one it comes with) so I have my DocumentRoot
as /var/www/example/public
. Everything works great! Setup a bunch of routes
for my main site and still smooth sailing.
Now moving to the subdomains, I have the below in my hhtpd.conf
<VirtualHost *:80>ServerName blog.example.co.uk
ServerAdmin [email protected]
DocumentRoot /var/www/example/public
<Directory /var/www/example/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/blog.example.co.uk.error.log
LogLevel warn
CustomLog /var/log/blog.example.co.uk.access.log combined
</VirtualHost>
As you can see I'm pointing the subdomain to the same public folder since Laravel
should be able to see that blog.example.co.uk
was requested and perform its duties to route correctly.
I have the below routes for the subdomain
Route::group(['domain' => 'blog.example.co.uk'], function () {Route::get('/', '[email protected]')->name('blog-home');
Route::get('review', '[email protected]')->name('blog-review');
Route::get('author/{id}', '[email protected]')->name('blog-author')->middleware('blogGuard');
Route::get('category/{id}', '[email protected]')->name('blog-category')->middleware('blogGuard');
Route::get('post/{id}', '[email protected]')->name('blog-post')->middleware('blogGuard');
Route::get('press', '[email protected]')->name('blog-press');
Route::get('about', '[email protected]')->name('blog-about');
});
Unfortunately, going to blog.example.co.uk
still assumes that I requested www.example.co.uk
and routes using the logic for the main site. So blog.example
points to www.example
.
I have also used '{blog}.example.co.uk'
What am I doing wrong please
Cheers!
It looks like this is an issue with the order of operations of your routes, where it matches a route before it even reaches the subdomain route group. You should put everything into its own route group and that should solve the problem. As an alternative you could make sure your non-grouped routes are below your grouped routes and that might also solve the problem.