skip to Main Content

I am using a laravel app my domain is showing pages like: www.mysite.com/public/about

on every page there is a duplicate page with /public/page_route , how could I fix this from .htaccess file.

My  .htaccess file code:
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>
    
     RewriteEngine on
     RewriteCond %{HTTP_HOST} ^mysite.com$ [NC,OR]
     RewriteCond %{HTTPS} off
     RewriteRule ^(.*)$ www.mysite.com/$1 [NC,L,R=301]

    # 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>

 <Files ~ ".(env|json|config.js|md|gitignore|gitattributes|lock|editorconfig|yml|styleci.yml)$">
     Order allow,deny
     Deny from all
 </Files>

 Options -Indexes
 <Files ~ "(artisan|package.json|webpack.mix.js)$">
     Order allow,deny
     Deny from all
 </Files>

I am trying to make all routes should be open without public name in route like:

www.mysite.com/about

2

Answers


  1. Please make index.php file in root

    <?php
    
    use IlluminateContractsHttpKernel;
    use IlluminateHttpRequest;
    
    define('LARAVEL_START', microtime(true));
    
    /*
    |--------------------------------------------------------------------------
    | Check If The Application Is Under Maintenance
    |--------------------------------------------------------------------------
    |
    | If the application is in maintenance / demo mode via the "down" command
    | we will load this file so that any pre-rendered content can be shown
    | instead of starting the framework, which could cause an exception.
    |
    */
    
    if (file_exists($maintenance = __DIR__.'/storage/framework/maintenance.php')) {
        require $maintenance;
    }
    
    /*
    |--------------------------------------------------------------------------
    | Register The Auto Loader
    |--------------------------------------------------------------------------
    |
    | Composer provides a convenient, automatically generated class loader for
    | this application. We just need to utilize it! We'll simply require it
    | into the script here so we don't need to manually load our classes.
    |
    */
    
    require __DIR__.'/vendor/autoload.php';
    
    /*
    |--------------------------------------------------------------------------
    | Run The Application
    |--------------------------------------------------------------------------
    |
    | Once we have the application, we can handle the incoming request using
    | the application's HTTP kernel. Then, we will send the response back
    | to this client's browser, allowing them to enjoy our application.
    |
    */
    
    $app = require_once __DIR__.'/bootstrap/app.php';
    
    $kernel = $app->make(Kernel::class);
    
    $response = $kernel->handle(
        $request = Request::capture()
    )->send();
    
    $kernel->terminate($request, $response);
    

    =================

    also make .htaccess file in root

    <IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine On
    
    RewriteCond %{REQUEST_URI} !^/public/ 
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    
    
    
    RewriteRule ^(.*)$ /public/$1 
    #RewriteRule ^ index.php [L]
    RewriteRule ^(/)?$ public/index.php [L] 
    </IfModule>
    

    after this try accessing route without public

    Login or Signup to reply.
  2. Domain’s document root

    The domain name should point to the /public folder as if it were your "public_html" folder.

    Whenever a request is made to this domain, the index.php should be executed

    You need to place an .htaccess file in the /public folder that contains the lines you mentioned, especially these:

    # 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]
    

    Return the appropriate content due to Laravel routing

    This way, every URL called under a domain name will start from your /public/index.php, which builds up your Laravel app. Eventually, it will either run the appropriate route defined in the /routes or return a 404 blank page if no matching route is found.

    Conclusion

    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.

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search