0

I have two laravel 10 projects in the same domain cms and website i access the images and files from cms side that url show domain.com/cms/public/storage/pages/press/root_1683963515doc.pdf here i remove the public from url and the cms domain like domain.com/cms/public/login without move the file from the public folder how we change the url with out public and whic project htacces file is use to change the url here i write in cms htacces file like

RewriteEngine On
RewriteBase /cms/

RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ public/$1 [L]

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]

but it not working i put domain.com/cms/login show 404 page, any way to solve this issue?

Andrew
  • 840
  • 3
  • 17
  • 43

2 Answers2

1

Just add htaccess on public folder only not any other folder

I assuming you put project like

/public
├── cms
│   ├── index.php
│   └── favicon.ico
├── favicon.ico
├── storage (link)
├── index.php
└── .htaccess

For main issue domain.com/cms/login can't access that

From this above tree you like to use index.php inside cms folder use your one and only htaccess. So only edit you public folder htaccess may be you remove NC it my preference. or may be use Handle Authorization Header.

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

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^cms/(.*) /cms/index.php  [NC,L,QSA]

    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>

For Stroage use public folder and create symbolic link on public folder and every link is available there if you are store something sensitive

don't save on public folder save on like other folder below and access with using controller.

/storage
├── app
|    ├── public  (this created symbolic link if you use php artisan storage:link)
|    └── other 
└── 
....
Puneet Sharma
  • 307
  • 3
  • 9
0

Use public folder as Domain Root

It is important that the domain address does not point to the root project directory. Follow my instructions to make the domain point to the /public folder from the beginning, so you don't have to apply .htaccess redirection and your website will be much more secure.

More information about "How to config domain to Laravel app?" - StackOverflow Answer

The /public appears in the URL if your domain name points to your-project instead of your-project/public. Therefore, you can access your starting index.php with https://example.com/public/index.php.

# normal directory tree
server
├── your-project
│   ├── app
│   ├── routes
│   ├── public        <--- domain root
│   │   ├── index.php
│   │   └── .htaccess
│   ├── ...

If the root of your domain points to the /your-project/public folder and you can still access its content using the /public address, then either you have incorrectly set up the Route in the /routes/web.php file (perhaps created a group named "public" or added the /public URL part to every route), or your domain is pointing to the /your-project folder incorrectly.



Set .htaccess

# normal directory tree
server
├── your-project
│   ├── app
│   ├── routes
│   ├── public        <--- domain root
│   │   ├── index.php
│   │   └── .htaccess <--- must be inside the domain root folder
│   ├── ...
<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>

.htaccess from laravel/laravel starter application



Set storage to public

If the domain address already points to the /public folder, then from there you can access the /storage/app/public folder using a symbolic link. The symbolic link should be placed in the /public folder with the name "storage".

This way, when you access the /public/storage folder, you are actually entering the /storage/app/public folder. Since the domain address points to the /public folder, the storage folder becomes immediately accessible via the symbolic link with the URL http://example.com/storage.

How to can generate /public/storage symbolic link?
# will run from project root folder
php artisan storage:link

More information about storage folder on Laravel Framework - StackOverflow Answer

A virtual reference that associates a non-existent path with an existing one. In our example, we can associate the non-existent path public/storage with the physically existing storage/app/public directory. How? We have two options for this. Either use the php artisan storage:link command in Laravel specifically designed for this purpose, or manually set it up in the operating system. Obviously, the former is the more obvious solution.

# normal directory tree
server
├── your-project
│   ├── app
│   ├── routes
│   ├── public
│   │   ├── storage    <--- symbolic link (to /server/your-project/storage/app/public)
│   │   ├── index.php
│   │   └── .htaccess
│   ├── storage
│   │   ├── app
│   │   │   └── public <--- here can store your files (really folder)
│   │   ├── framework
│   │   └── logs
│   ├── ...

If you create the symbolic link, it will work as expected. For example, you can access the storage/app/public/images/test.png file through a virtual path: public/storage/images/test.png.

More information about symbolic links - StackOverflow Answer

rozsazoltan
  • 2,831
  • 2
  • 7
  • 20