Welcome Guest, Not a member yet? Register   Sign In
Codeigniter 4 routing not working on live server
#1

(This post was last modified: 10-22-2020, 02:50 AM by adil.)

hi,
i am developing a website.its working fine on local.But when i uploaded it to windows server,only default controller working.getting 
"Not Found
The requested document was not found on this server."

document root is httpdocs/public.
here is the htaccess  on public.
Code:
# Disable directory browsing
Options All -Indexes

# ----------------------------------------------------------------------
# Rewrite engine
# ----------------------------------------------------------------------

# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On

    # If you installed CodeIgniter in a subfolder, you will need to
    # change the following line to match the subfolder you need.
    # http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
    # RewriteBase /

    # Redirect Trailing Slashes...
    RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)/$ /$1 [L,R=301]
        

    # Rewrite "www.example.com -> example.com"
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

    # Checks to see if the user is attempting to access a valid file,
    # such as an image or css document, if this isn't true it sends the
    # request to the front controller, index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
      

    # Ensure Authorization header is passed along
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    ErrorDocument 404 index.php
</IfModule>

# Disable server signature start
    ServerSignature Off
# Disable server signature end
and htacces on httpdocs
Code:
<IfModule mod_rewrite.c>
DirectoryIndex /public/index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|assets|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./public/index.php/$1 [L,QSA]
</IfModule>
and here is the route
Code:
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true);
$routes->match(['get', 'post'], '/Login', 'Home::login');
Reply
#2

Hi adil

you should have to do anything to .htaccess until you move to https. Keep it simple get http working first .

probably more important is set up of directories and the hosting

what is inside "public" should be inside where the hosting is looking , htdocs or public_html or equivalent.

If you packed everything else say into a directory called "CI4" and this directory is at a level above public_html on your live sever then you edit index.php to:

$pathsPath = realpath(FCPATH . '../CI4/app/Config/Paths.php');





try a manual route:

in Routes.php add
Code:
$routes->get('test','Home::test');

In Home Controller add:

Code:
public function test()

{
echo "hello world, from home controller";

}

then go in web browser to

http://domain.com/test
CMS CI4     I use Arch Linux by the way 




Arch Book
Reply
#3

Hi captain-sensible,
thank you for the reply.its still the same.
error "Not Found The requested document was not found on this server."
my directory structure is :
httpdocs
app
public
system
writable
vendor
.env
.htaccess

When i change document root from httpdocs/public to httpdocs and add public/index.php/ to url it works
Reply
#4

(This post was last modified: 10-24-2020, 02:31 AM by captain-sensible. Edit Reason: added images via full edit )

i notice i slight duplication there ; you can try this experiment take out system and place it where it you can be put back . you should find it still works since composer is autoloading whats in vendor.

you should not have httpdocs and public directory . The contents of what  inside public should be inside the web root, where the server is looking for index.php, the rest should be bundled and placed at a level outside web root.

have a look at attached images; my main hosting has 4 webs but on sub domain set up ; i wanted to see how it would work with one app in one hosting.


Look at the directory names.

there is no "public" directory thats because public_html "represents it" in other  words, you need to go INSIDE your public directory and zip up contents ONLY, you then go to inside your live web root eg public_html and extract contents of the zip file you just uploaded . Everything else can be put into a directory in my case CI4 and as i said i simply edited index.php to take account of that .

You can play for free on 000webhost : https://www.000webhost.com/

to prove my approach look at :

https://ghanahomefood.000webhostapp.com/


Can you get any screen shots

Attached Files Thumbnail(s)
       
CMS CI4     I use Arch Linux by the way 




Arch Book
Reply
#5

also probably set $routes->setAutoRoute(false);

if you have defined routes in app/Config/Routes.php
CMS CI4     I use Arch Linux by the way 




Arch Book
Reply
#6

Can you post a screen shot of your server's filesystem structure?

Check first if you do have mod_rewrite enabled. Then your web assets should not be situated in the httpdocs folders but inside the public folder. So the htaccess on httpdocs can be removed.
Reply
#7

He has everything under a ci4 folder with public in the root.

Needs to change path settings from what I see og the directory structure.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#8

Hi thanks for the reply guys.
I solved the problem.
I added some lines to web config file. its on windows server.
Code:
<asp scriptErrorSentToBrowser="true" />
    <rewrite>
    <rules>
        <rule name="RuleRemoveIndex" stopProcessing="true">
            <match url="^(.*)$" ignoreCase="false" />
           <conditions logicalGrouping="MatchAll">
                <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
            </conditions>
            <action type="Rewrite" url="index.php?url={R:1}" appendQueryString="true" />
        </rule>
       
    </rules>
    </rewrite>

</system.webServer>
<system.webServer>
        <staticContent>
            <mimeMap fileExtension=".webm" mimeType="video/webm" />
            <mimeMap fileExtension=".mp4" mimeType="video/mpeg" />
            <mimeMap fileExtension=".webp" mimeType="image/webp" />
        </staticContent>
    </system.webServer>



<system.web>
    <customErrors mode="Off" />
    <compilation debug="true" />
</system.web>

and all the directory structure is same as the original ci4 structure. I changed root dir to httpdocs to httpdocs/public.
Now its working fine.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB