How to make .htaccess file redirect to public folder on cPanel PHP

Viewed 20

I wrote a .htaccess file that redirects to the public folder on my local machine but does not on my Cpanel after being uploaded to the server.

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_URI} !/public
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d

# Direct all requests to /public folder
RewriteRule ^(.*)$ /public/index.php?url=$1 [L]
</IfModule>

How do I make it redirect online on my Cpanel?

2 Answers

You don't use an htaccess file to redirect to the public folder on local or your server. You point the domain to the /public folder.

In cpanel, when adding a domain, you specify the path to the /public folder as the path for the domain to use.

Use this into root folder

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

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]

RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1 

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php

#caching
<FilesMatch ".(jpg|jpeg|png|gif|swf)$">
<IfModule mod_headers.c>
    Header set Cache-Control "max-age=604800, public"
</IfModule>
</FilesMatch>


<FilesMatch ".(xml|txt|css|js)$">
<IfModule mod_headers.c>
    Header set Cache-Control "max-age=604800, proxy-revalidate"
</IfModule>
</FilesMatch>

Then create another file name server.php

Put this code into server.php

<?php

/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <taylor@laravel.com>
 */

$uri = urldecode(
    parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);

// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
    return false;
}

require_once __DIR__.'/public/index.php';
Related