Welcome Guest, Not a member yet? Register   Sign In
Remove /public from URLs
#1
Question 

Hi everyone, I was wondering if there is any official documentation / way to remove the public from the urls.
I remember CI3 having a separate section where it stated the lines to be added to the .htaccess file in order to remove the index.php and keep all the benefits without compromising security or design. Couldn't get to the same information when migrating to CI4.
Any help on this matter is really appreciated.
Thank you all.
Reply
#2

Hi there,

You will find htaccess and index.php files under Public folder. Move them to the main folder and open the index.php with your code editor. Line 20 (or sth) you will see

Code:
$pathsConfig = FCPATH . '../app/Config/Paths.php';

here remove the ../ at the beginning and it will be 'app/Config/Paths.php';

That's all.

PS : Don't forget to remove index.php string in your App file under Config as usual (Line 39):

Code:
public $indexPage = '';
Reply
#3

(This post was last modified: 02-15-2022, 12:45 AM by luckmoshy.)

I don't know why many people suggest removing index from public to outside that is not the right way only .htaccess can work for Appache here you go don't touch anything just copy this to .htaccess game over (here we have hidden public folder with.htaccess)
Code:
RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
    RewriteRule ^ %1 [L,NE,R=302]
    RewriteRule ^((?!public/).*)$ public/$1 [L,NC]

then go to App/config/app just remove (index.php) to the plain game over enjoy your c_i 4+ whether local or on live server
Codeigniter First, Codeigniter Then You!!
yekrinaDigitals

Reply
#4

(This post was last modified: 02-15-2022, 12:47 PM by pippuccio76.)

(02-15-2022, 12:40 AM)luckmoshy Wrote: I don't know why many people suggest removing index from public to outside that is not the right way only .htaccess can work for Appache here you go don't touch anything just copy this to .htaccess game over (here we have hidden public folder with.htaccess)
Code:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]

then go to App/config/app just remove (index.php) to the plain game over enjoy your c_i 4+ whether local or on live server
How can i start project in localhost , with php spark serve or simply view the folder in localhost ? .htaccess must be only this  5 rows ? if i use .env file must change  public $indexPage = ''; ?
Reply
#5

(02-15-2022, 12:43 PM)pippuccio76 Wrote:
(02-15-2022, 12:40 AM)luckmoshy Wrote: I don't know why many people suggest removing index from public to outside that is not the right way only .htaccess can work for Appache here you go don't touch anything just copy this to .htaccess game over (here we have hidden public folder with.htaccess)
Code:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]

then go to App/config/app just remove (index.php) to the plain game over enjoy your c_i 4+ whether local or on live server
How can i start project in localhost , with php spark serve or simply view the folder in localhost ? .htaccess must be only this  5 rows ? if i use .env file must change  public $indexPage = ''; ?

There is one (.htaccess) in public just leave it Create a new .htaccess in the app root take that code above and paste in it now you are right to go whatever way Php spark or else
Codeigniter First, Codeigniter Then You!!
yekrinaDigitals

Reply
#6

(02-15-2022, 12:40 AM)luckmoshy Wrote: I don't know why many people suggest removing index from public to outside that is not the right way only .htaccess can work for Appache here you go don't touch anything just copy this to .htaccess game over (here we have hidden public folder with.htaccess)
Code:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]

then go to App/config/app just remove (index.php) to the plain game over enjoy your c_i 4+ whether local or on live server
its not working for 4.4.3 version can you suggest ant thing
Reply
#7

See manual https://codeigniter4.github.io/CodeIgnit...x-php-file
It works
Reply
#8

.htaccess
Code:
# Remove index.php from URL
RewriteCond %{HTTP:X-Requested-With}    !^XMLHttpRequest$
RewriteCond %{THE_REQUEST}              ^[^/]*/index\.php [NC]
RewriteRule ^index\.php(.*)$            $1 [R=301,NS,L]
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB