{"id":44225,"date":"2024-03-10T11:11:39","date_gmt":"2024-03-10T11:11:39","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=44225"},"modified":"2024-03-10T11:11:39","modified_gmt":"2024-03-10T11:11:39","slug":"laravel-tutorials-how-to-run-laravel-projects-in-subdirectory","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/laravel-tutorials-how-to-run-laravel-projects-in-subdirectory\/","title":{"rendered":"Laravel Tutorials: How to run Laravel Projects in Subdirectory"},"content":{"rendered":"\n<p>The issue you&#8217;re experiencing is common with Laravel applications when deployed in a subdirectory. By default, Laravel&#8217;s entry point is the <code>public<\/code> directory, which means you need to point your web server&#8217;s document root to the <code>public<\/code> directory of your Laravel application for it to work correctly. However, in a shared hosting environment or when working within a subdirectory, you may not have the flexibility to change the server configuration easily. Here are a couple of solutions to make your Laravel application <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Option 1: Modify the <code>.htaccess<\/code> File<\/h3>\n\n\n\n<p>You can use an <code>.htaccess<\/code> file to rewrite URLs so that requests to <code>https:\/\/www.bangaloreorbit.com\/events\/<\/code> are served by the <code>public<\/code> directory of your Laravel project. If you don&#8217;t already have an <code>.htaccess<\/code> file in your <code>events<\/code> directory, create one. Then, add the following rules:<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">&lt;IfModule mod_rewrite.c&gt;\r\n    RewriteEngine On\r\n\r\n    <span class=\"hljs-comment\"># Redirect Trailing Slashes If Not A Folder...<\/span>\r\n    RewriteCond %{REQUEST_FILENAME} !-d\r\n    RewriteCond %{REQUEST_URI} (.+)\/$\r\n    RewriteRule ^ %<span class=\"hljs-number\">1<\/span> &#91;L,R=<span class=\"hljs-number\">301<\/span>]\r\n\r\n    <span class=\"hljs-comment\"># Handle Front Controller...<\/span>\r\n    RewriteCond %{REQUEST_URI} !^\/<span class=\"hljs-keyword\">public<\/span>\/\r\n    RewriteRule ^(.*)$ <span class=\"hljs-keyword\">public<\/span>\/$<span class=\"hljs-number\">1<\/span> &#91;L,QSA]\r\n\r\n    <span class=\"hljs-comment\"># Handle Authorization Header<\/span>\r\n    RewriteCond %{HTTP:Authorization} .\r\n    RewriteRule .* - &#91;E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]\r\n&lt;\/IfModule&gt;\r\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Option 2: Change Document Root (If Possible)<\/h3>\n\n\n\n<p>If you have access to modify the document root setting for your domain or subdomain (this is possible in some hosting environments), change the document root to point directly to the <code>public<\/code> directory of your Laravel application:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>From: <code>\/home\/professnow\/public_html\/orbit\/bangaloreorbit.com\/events<\/code><\/li>\n\n\n\n<li>To: <code>\/home\/professnow\/public_html\/orbit\/bangaloreorbit.com\/events\/public<\/code><\/li>\n<\/ul>\n\n\n\n<p>This change ensures that the web server serves the Laravel application directly from the correct entry point.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Option 3: Move Laravel Public Directory Content<\/h3>\n\n\n\n<p>If you cannot change the document root, another approach is to move the contents of the <code>public<\/code> directory to the root of your <code>events<\/code> directory and update paths accordingly. This method requires adjusting the <code>index.php<\/code> file in your new <code>public<\/code> location to correctly point to the <code>autoload.php<\/code> and <code>app.php<\/code> files.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Move all files from <code>public<\/code> to the <code>events<\/code> root directory.<\/li>\n\n\n\n<li>Open <code>index.php<\/code> in the new location and update the paths to <code>autoload.php<\/code> and <code>app.php<\/code> to reflect their new paths.<\/li>\n<\/ol>\n\n\n\n<p>For example, change:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"918\" height=\"506\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2024\/03\/image-141.png\" alt=\"\" class=\"wp-image-44226\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2024\/03\/image-141.png 918w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2024\/03\/image-141-300x165.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2024\/03\/image-141-768x423.png 768w\" sizes=\"auto, (max-width: 918px) 100vw, 918px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>The issue you&#8217;re experiencing is common with Laravel applications when deployed in a subdirectory. By default, Laravel&#8217;s entry point is the public directory, which means you need to point your&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-44225","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/44225","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=44225"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/44225\/revisions"}],"predecessor-version":[{"id":44227,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/44225\/revisions\/44227"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=44225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=44225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=44225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}