How to redirect all Laravel routes to a new subdomain using Laravel routing system?
P粉310931198
2023-09-02 11:45:56
<p>Redirect all Laravel routes to the same route, but change the base URL. </p>
<p>I want to move my Laravel project from domain to subdomain
What is the best way to redirect all requests on the last domain to the same new subdomain. </p>
<p>For example, if a user sends a request to this URL</p>
<pre class="brush:php;toolbar:false;">mydomain.com/page/1</pre>
<p>Redirect to this URL</p>
<pre class="brush:php;toolbar:false;">subdomain.mydomain.com/page/1</pre>
<p>I prefer to handle it inside the Laravel project. Not an NGINX configuration. </p>
To handle this at the Laravel level you can use middleware. Middleware provides a convenient mechanism to inspect and filter HTTP requests entering your application.
Here are examples of how you can do this.
First, create a new middleware by running the following command:
Next, open the newly created file app/Http/Middleware/SubdomainRedirectMiddleware.php and add the redirection logic to the handle method:
Then, you need to register this middleware. Open app/Http/Kernel.php and add the following lines to the routeMiddleware array:
▽This is a reference https://www.w3schools.in/laravel/middleware