{"id":916,"date":"2024-07-22T17:45:15","date_gmt":"2024-07-22T17:45:15","guid":{"rendered":"https:\/\/www.cmsgalaxy.com\/blog\/?p=916"},"modified":"2024-07-22T17:45:15","modified_gmt":"2024-07-22T17:45:15","slug":"how-to-secure-your-laravel-project-to-run-exclusively-on-a-specific-domain","status":"publish","type":"post","link":"https:\/\/www.cmsgalaxy.com\/blog\/how-to-secure-your-laravel-project-to-run-exclusively-on-a-specific-domain\/","title":{"rendered":"How to Secure Your Laravel Project to Run Exclusively on a Specific Domain"},"content":{"rendered":"\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.devopsconsulting.in\/blog\/wp-content\/uploads\/2024\/07\/An-illustration-of-a-Laravel-project-being-secured-to-run-exclusively-on-a-specific-domain.-.webp\" alt=\"\" class=\"wp-image-1099\"\/><\/figure>\n\n\n\n<p>To protect your Laravel project so that it can only run on a specific domain, you can implement domain-specific validation within your application. Here are some steps to achieve this:<\/p>\n\n\n\n<p><strong>1. Environment Configuration<\/strong><\/p>\n\n\n\n<p>First, define the allowed domain in your\u00a0<code>.env<\/code>\u00a0file. Add a new variable:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">APP_NAME='eventmotoshare'<\/pre>\n\n\n\n<p><strong>2. Middleware for Domain Check<\/strong><\/p>\n\n\n\n<p>Create a middleware to check the domain.<\/p>\n\n\n\n<p><strong>Create Middleware<\/strong><\/p>\n\n\n\n<p>Run the following Artisan command to create a middleware:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">php artisan make:middleware DomainCheck<\/pre>\n\n\n\n<p><strong>Middleware Logic<\/strong><\/p>\n\n\n\n<p>Edit the generated middleware file\u00a0<code>app\/Http\/Middleware\/DomainCheck.php<\/code>\u00a0to include the domain check logic:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php<br><br>namespace App\\Http\\Middleware;<br><br>use Closure;<br>use Illuminate\\Http\\Request;<br><br>class DomainCheck<br>{<br>    \/**<br>     * Handle an incoming request.<br>     *<br>     * @param  \\Illuminate\\Http\\Request  $request<br>     * @param  \\Closure  $next<br>     * @return mixed<br>     *\/<br>    public function handle(Request $request, Closure $next)<br>    {<br>        $allowedDomain = env('APP_NAME');<br><br>        if ($request->getHost() !== $allowedDomain) {<br>            abort(403, 'Unauthorized action.');<br>        }<br><br>        return $next($request);<br>    }<br>}<\/pre>\n\n\n\n<p><strong>3. Register Middleware<\/strong><\/p>\n\n\n\n<p>Register the middleware in\u00a0<code>app\/Http\/Kernel.php<\/code>\u00a0under the\u00a0<code>web<\/code>\u00a0middleware group:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">protected $middlewareGroups = [<br>    'web' => [<br>        \/\/ Other middleware,<br>        \\App\\Http\\Middleware\\DomainCheck::class,<br>    ],<br><br>    'api' => [<br>        \/\/ Other middleware<br>    ],<br>];<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>To protect your Laravel project so that it can only run on a specific domain, you can implement domain-specific validation<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-916","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/916","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/comments?post=916"}],"version-history":[{"count":1,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/916\/revisions"}],"predecessor-version":[{"id":917,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/916\/revisions\/917"}],"wp:attachment":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/media?parent=916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/categories?post=916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/tags?post=916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}