0

I'm trying to setup a Laravel app in a subfolder of a WordPress website.

The idea is that when a request goes to example.com/{anything}, the WordPress website will respond but if the request is made on example.com/get-started/*, then the Laravel website will handle the request.

I have found a few similar questions online but none of them is working for me. The recent I have tried is this one from laracasts.

My root htaccess file is

# Block the include-only files.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^wp-admin/includes/ - [F,L]
RewriteRule !^wp-includes/ - [S=3]
RewriteRule ^wp-includes/[^/]+\.php$ - [F,L]
RewriteRule ^wp-includes/js/tinymce/langs/.+\.php - [F,L]
RewriteRule ^wp-includes/theme-compat/ - [F,L]
</IfModule>

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !^/get-started [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

My htaccess from 'get-started' folder is

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^public
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

htaccess from 'get-started/public' folder is

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

When I open example.com/get-started, it displayes laravel's default 404 page which tells me that the redirection is working but the laravel app doesn't have a corresponding route.

So I give you my web.php file

Route::redirect('/', '/step/1')->name('home');

Route::match(['GET', 'POST'], '/1', [\App\Http\Controllers\HomeController::class, 'step1'])->name('1');
Route::match(['GET', 'POST'], '/2', [\App\Http\Controllers\HomeController::class, 'step2'])->name('2');
Route::match(['GET', 'POST'], '/3', [\App\Http\Controllers\HomeController::class, 'step3'])->name('3');

I'm using this preset from Laravel.

4
  • Presumably you have another .htaccess file at /get-started/public/.htaccess? Please update your question to include the contents of this file also. Commented Oct 6, 2022 at 10:30
  • @MrWhite, It is the default .htaccess file shipped with laravel that's why I didn't upload it initially but I have edited my question to include it as well. Commented Oct 7, 2022 at 21:48
  • Your .htaccess files look OK. There are a few things that could be tidied up, but nothing that would change the working. This looks like a Laravel routing issue. However, I'm not sure what you mean by "I can prefix all the URLs but then the assets doesn't load."? Any assets associated with your Laravel project should reference the /get-started/ subdir, assuming that is where they are located... As in your example, example.com/get-started/build/assets/main.3843b6a5.js would get rewritten to example.com/get-started/public/build/assets/main.3843b6a5.js - which I assume is the intention? Commented Oct 9, 2022 at 12:31
  • @MrWhite I have updated the question to simplify it a bit (Check routes/web.php file). When I run the standalone Laravel app on the server, it works fine which leads me to believe that Laravel's routing file is fine. But when I set it up as a subdirectory on the WordPress website, it shows 404. Any suggestion about how can I make it work? Commented Oct 9, 2022 at 17:41

1 Answer 1

0

I haven't set this up but I think of 2 options.

  1. Change in Laravel config the path of the public folder to /get-started/

  2. Much like as wordpress captures path when you install it in sub-directory, you should add somewhere to your .htaccess in get-started/public before the relevant RewriteCond(s):

RewriteBase /get-started/

Hope it works.

Sign up to request clarification or add additional context in comments.

5 Comments

may be the need of adding an Alias is also required?
FWIW the .htaccess files look OK to me. You should not add RewriteBase directives in either of the subdirectory .htaccess files IMO (that would simply create an unnecessary dependence on the parent directory). However, if you do add a RewriteBase directive to the /get-started/public/.htaccess file then it would need to be RewriteBase /get-started/public/ (trailing slash optional), not RewriteBase /get-started/, since index.php is located inside the public subdirectory, not the parent directory.
@OMiShah How would you intend to implement the Alias? If you have access to the server config then you could create an Alias from /get-started to /get-started/public - but that is instead of the /get-started/.htaccess file, not as well as. (But if you have access to the server config then you'd probably manage this differently to begin with.)
Yes, the alias needs to be added in the server conf file.
Adding this doesn't make any difference. Also, @MrWhite is right, I can't make changes in the server conf file. I only have access to the cPanel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.