Dynamic subdomain Laravel - laravel

I want to know, how to create dynamic subdomain with laravel 5.6 and wampserver
My code in web route for test
Route::group(array('domain' => '{subdomain}.test.local'), function () {
Route::get('/', function ($subdomain) {
$name = DB::table('users')->where('username', $subdomain)->get();
dd($name);
});
});
I do one parameter on my vhost and httpd-vhosts. After I added alias name. Result here
<VirtualHost *:80>
ServerName test.local
ServerAlias *.test.local
DocumentRoot "c:/wamp64/www/test"
<Directory "c:/wamp64/www/test/">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from all
</Directory>
</VirtualHost>
In my browser I try to use test.test.local for try if subdomain is correct but doesn't work.
Note: I would like to have variable subdomain
Thank you for your answer

Related

prevent loading subdomain when using www

I have a Laravel project, where i am managing domain and subdomain altogether. That domain and subdomains has different functionalities. When I'm loading my main domain like this https://myclearmargin.com/ without using www it is working perfectly. But when I am adding www https://www.myclearmargin.com/ to the url, it's loading the content of a sub-domain. I am unable to find any solution to this, nor I understand where the problem occurs/where should i dig into. Here is my code on routes/web.php
<?php
$siteUrl = env("APP_URL");
$subdomainRoutes = function () {
// all sub-domain routes
};
$mainRoutes = function () {
// all domain routes
}
Route::group(array('domain' => '{account}.' . $siteUrl), $subdomainRoutes);
Route::group(array('domain' => $siteUrl), $mainRoutes);
My project was deployed on apache (centos 8) server. and here is my apache settings for the project.
domain.conf:
<VirtualHost *:80>
DocumentRoot /var/www/html/MyClearMargin/public
ServerName myclearmargin.com
ServerAlias www.myclearmargin.com
<Directory "/var/www/html/MyClearMargin/public">
Allowoverride All
Options Indexes FollowSymLinks
Require all granted
</Directory>
ErrorLog /var/www/html/log/error.log
RewriteEngine on
RewriteCond %{SERVER_NAME} =myclearmargin.com [OR]
RewriteCond %{SERVER_NAME} =www.myclearmargin.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
subdomain.conf:
VirtualHost *:80>
DocumentRoot /var/www/html/MyClearMargin/public
ServerAlias *.myclearmargin.com
<Directory "/var/www/html/MyClearMargin/public">
Allowoverride All
Options Indexes FollowSymLinks
Require all granted
</Directory>
ErrorLog /var/www/html/log/error.log
</VirtualHost>
Can anyone help me out? I am struggling for this for months. Let me know if you need for informations.
TIA

How to point multiple laravel projects on same domain?

I want to create two Laravel applications like below:
app1.domain.com -> points to app1 laravel. Here I am allowing multiple subdomains like wildcard subdomains.
app1.domain.com/blog -> should point to blog laravel.
Database is the same for both applications, and session will be the same.
How can point these two like above?
I have pointed like below
<VirtualHost *:80>
DocumentRoot "C:\wamp64\www\app1\public"
ServerName app1.domain.local
ServerAlias *.domain.local
<Directory "C:\wamp64\www\app1\public">
Options Indexes FollowSymLinks
allow from all
order allow,deny
AllowOverride All
</Directory>
## Path to laravel sub-folder
Alias /blog/ "C:\wamp64\www\blog\public"
<Directory "C:\wamp64\www\blog\public">
AllowOverride All
</Directory>
</VirtualHost>
Using above config, app1 is working fine but blog application does not work. It shows forbidden error You don't have permission to access /blog/ on this server.
Solution:
<VirtualHost *:80>
DocumentRoot "C:\wamp64\www\app1\public"
ServerName app1.domain.local
ServerAlias *.domain.local
## Path to laravel sub-folder
Alias /blog/ "C:\wamp64\www\blog\public"
<Directory "C:\wamp64\www\blog\public">
AllowOverride All
</Directory>
<Directory "C:\wamp64\www\app1\public">
Options Indexes FollowSymLinks
allow from all
order allow,deny
AllowOverride All
</Directory>
</VirtualHost>
Now working fine but with in the blog application assets path not working correctly. assets pointing from main domain instead of subfolder.
it should be like
http://app1.domain.local/blog/assets/js/login.min.js?id=c818c905d151b67566ee
but loading from http://app1.domain.local/assets/js/login.min.js?id=c818c905d151b67566ee

Laravel route getting ruined when trying to reach HTTP request

Currently I have successfully setup my Laravel Passport API
Using Laravel 5.8.
I have this httpd-vhosts.conf config for my api
PORT: 80
<VirtualHost *:80>
ServerName <sub-domain>.<domain>.com
ServerAlias <sub-domain>.<domain>.com
Redirect permanent / https://<sub-domain>.<domain>.com
</VirtualHost>
PORT: 443
<VirtualHost *:443>
DocumentRoot "/opt/lampp/htdocs/api_tk/public"
<Directory "/opt/lampp/htdocs/api_tk/public">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ServerName <sub-domain>.<domain>.com
ServerAlias <sub-domain>.<domain>.com
ErrorLog "logs/API-error_log"
CustomLog "logs/API-access_log" common
ProxyPreserveHost On
ProxyRequests Off
ProxyPassMatch /fingerprint http://localhost:5000
ProxyPassReverse /fingerprint http://localhost:5000
RequestHeader set X-Forwarded-Proto https
RequestHeader set X-FOrwarded-Port 443
SSLEngine on
SSLCertificateFile "/opt/lampp/htdocs/ssl_key/svs-file.crt"
SSLCertificateKeyFile "/opt/lampp/htdocs/ssl_key/private_new.key"
SSLCACertificateFile "/opt/lampp/htdocs/ssl_key/svs-bundle-file.crt"
</VirtualHost>
443 is working fine I can see my HTTPS SSL Lock sign on my browser
But
When I try to do HTTP request
Laravel API always getting ruined.
I have route like this
https://..com/api/login
and this is working fine
But when I tried to do HTTP request like this
http://<sub-domain>.<domain>.com/api/login
it always end up to
https://<sub-domain>.<domain>.comapi/login
Where the slash is missing. This is because of the redirect permanent on my PORT 80 config.
I have this route for my api. (api.php)
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::post('/timekeeping','Auth\Api\AuthController#timekeeping');
Route::post('/login','Auth\Api\AuthController#login');
Route::middleware('auth:api')->group(function () {
Route::post('/timekeeping_app','Auth\Api\AuthController#timekeeping_app');
Route::post('/logout','Auth\Api\AuthController#logout');
Route::post('/register','Auth\Api\AuthController#register');
Route::post('/show_dtr_list','Auth\Api\AuthController#show_dtr_list');
Route::post('/update','Auth\Api\AuthController#update');
Route::post('/delete','Auth\Api\AuthController#delete');
Route::post('/search_user','Auth\Api\AuthController#search_user');
Route::get('/current_time','Auth\Api\AuthController#current_time');
});
How can I prevent this?
UPDATE
Tried to do edit my vhosts config like this
Redirect permanent "/" "https://<sub-domain>.<domain>.com/"
And
Redirect permanent / https://<sub-domain>.<domain>.com\/
But this gives me an output like this
try do add slash under your apache virtual host settings:
<VirtualHost *:80>
ServerName <sub-domain>.<domain>.com
ServerAlias <sub-domain>.<domain>.com
# fix below
Redirect permanent / https://<sub-domain>.<domain>.com/
</VirtualHost>

Create dynamic sub-domain as per username in Laravel

I'm creating web application which needs dynamic domain per user.
Let say I've 5 users in my DB such as.
1) wanda
2) edward
3) rick
4) maurice
5) kristin
Now I've a domain as example.com.
I want to get data of that 5 users as per his domain.
1) wanda.example.com
2) edward.example.com
3) rick.example.com
4) maurice.example.com
5) kristin.example.com
route.php
Route::group(['domain' => '{user}.example.com'], function () {
Route::get('/', function ($user) {
echo $user;
exit;
});
});
I'm using Xampp, Apache server so I configure httpd-vhosts file as
C:\xampp\apache\conf\extra\httpd-vhosts
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/myproject"
ServerName example.com
ServerAlias *.example.com
DirectoryIndex index.php
<directory "C:/xampp/htdocs/myproject">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from all
</directory>
</VirtualHost>
C:\Windows\System32\drivers\etc\hosts file
127.0.0.1 example.com
127.0.0.1 *.example.com
When I access user.example.com I'm Getting this error.
I want this type of dynamic domain as works.
http://wanda.thewallchat.com/
http://edward.thewallchat.com/
You cannot write * for all in <VirtualHost *:80>
you need to specify each alias which you want to use in your system.
change
ServerAlias *.example.com
to
ServerAlias example.com wanda.example.com edward.example.com rick.example.com maurice.example.com kristin.example.com
Don't forget to restart apache.
If you want to setup on live server configure wildcard subdomain.
ServerAlias *.example.com

404 Error in laravel 5.1 Wamp

I am beginner in Laravel. I used Laravel 5.1 in Wamp Server (Apache/2.4.9 (Win64) PHP/5.5.12)
I received the 404 Not Found Error, when I called the page like
http://localhost/laravel/public/login
Route::get('login', function() {
//return View::make('login');
return 'Hello World'; });
vhost.conf code
ServerAdmin local#gmail.com
DocumentRoot "C:\wamp\www\laravel\public"
ServerName localhost
ErrorLog "logs/AuthorityController.www-error.log"
CustomLog "logs/AuthorityController.www-access.log" common
<Directory "C:\wamp\www\laravel\public">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
Try this: http://localhost/login
may be it helps you, i hope :). And check that you vhost in appache is set to .../your_laravel_project/public folder
UPDATE
Go to C:\Windows\System32\drivers\etc\hosts and add line at the bottom:
127.0.0.1 myproject.dev
Then, in your appache httpd.conf file you shoud uncoment line:
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
BONUS: You can also switch directory with you projects in lines (for instance i have my projects in 'F:/Code'):
DocumentRoot "F:/Code"
<Directory "F:/Code">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
Next step, in your httpd-vhosts.conf you should have:
<VirtualHost myproject.dev>
DocumentRoot "F:\Code\myproject\public"
ServerName myproject.dev
ServerAlias myproject.dev
<Directory "F:\Code\myproject\public">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Then reset appache. And when you go to your browser and put http://myproject.dev you should see your page :)
You don't need to do that anymore. Don't include that. What you do is put a view file (your 404 error view) called 404.blade.php in your resources/views/errors folder and Laravel will handle 404 errors for you.

Resources