Home > Software design >  Unable to open subdirectory laravel installation
Unable to open subdirectory laravel installation

Time:03-10

In my /public_html/ I have installed an WordPress site. Now I have installed an laravel application inside /public_html/app/.

Then in /public_html/app/.htaccess I have added:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

In /public_html/app/public/.htaccess I have added:

<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On    
    RewriteBase /app/

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule . index.php [L]
</IfModule>

When I trying to open https://example.com/app/ I've got error 404. When I try to open directly https://example.com/app/public I've got half working site because it is searching for the css/images in https://example.com/.

What is need to be changed in the htaccesss in order to work.

The goal is to have a button on the main WP site and when I click it to load the laravel site.

CodePudding user response:

RewriteBase /app/

You need to remove the RewriteBase directive. This ends up rewriting the request to /app/index.php, when it should be /app/public/index.php. The default is to rewrite to the current directory (the directory that contains the .htaccess file), so the RewriteBase directive is not required here.

(Or, you could set this "correctly" to RewriteBase /app/public - but that is not necessary and would then hardcode this installation to the /app directory.)

I've got half working site because it is searching for the css/images in https://example.com/

It depends on where your images are. If images are located at /app/public/assets/images/myimage.jpg then you should be referencing your images using a root-relative URL (starting with a slash), excluding the public directory, eg. href="/app/assets/images/myimage.jpg".


UPDATE:

is this means that I now have to manually edit all images, links, buttons, etc on the site in order to add /app/... in front of the assets?

Ordinarily, yes. In the same way you have presumably added /app/ before all your internal links to your pages. (?)

Logo for example - <img src="/assets/main/img/logo.png">

However, since you are using a root-relative URL and your Laravel assets are in a known location, then you could workaround this by implementing a rewrite in the root (WordPress) .htaccess file to rewrite your Laravel assets to the correct location (presumably /app/public/...).

However, this does mean that you cannot then have an /assets subdirectory in the root (without implementing additional filesystem checks), nor have a WordPress URL that starts /assets, since it will conflict and won't be accessible.

For example, at the top of the /.htaccess file, before any other WordPress directives you could do something like the following:

# Rewrite Laravel assets to the correct location
RewriteRule ^assets/.  app/public/$0 [L]

# BEGIN WordPress
:

Where $0 is a backreference that contains the entire URL-path that is matched by the RewriteRule pattern.

Now, any request for /assets/<something> will be internally rewritten to /app/public/assets/<something>. /assets/ itself won't be rewritten.

  • Related