Removing .php part in URL on a multisite Codeignit

2019-09-16 21:12发布

问题:

I have a Codeigniter-based system with two apps in it, app and site, which share a single system folder. The former has the backend, latter has the front-end. Because of this, index.php defaults to site and there is a separate app.php to access the backend.

http://localhost/subdirectory/ goes to the frontend, while to access the backend I have to write http://localhost/subdirectory/app.php.

I have referred to the User Guide on how to remove the index.php thing, and it works on the frontend. Say, http://localhost/subdirectory/pages/about successfully goes to the site folder's pages class with an argument of about.

The issue is when I try to access the login page, the URI points to http://localhost/subdirectory/app.php/auth/login. Entering http://localhost/subdirectory/app/auth/login returns the 403 Forbidden page, which I presume to be the index.html on the folder UPDATE: discovered that it's due to the .htaccess present on app.

How can I remove the app.php part?

Here's my application structure:

app
| - cache, config, controllers, views, and other related CI folders
site
| - cache, config, etc, like app
system
| - CI's system folder
app.php
index.php
.htaccess

And here's my current .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

RewriteRule ^(app)/(.*)$ app.php/$1 [NC,L]
RewriteRule ^(.*)$ index.php/$1 [NC,L]

That doesn't work.

Any help? Thank you!

UPDATE: 403 Forbidden the shows up is caused by .htaccess with a content of "Deny from All", not only because of index.html. I don't know if this helps.

回答1:

Take a look at your .htaccess carefully:

RewriteRule ^(app)/(.*)$ app.php/$1 [NC,L]

In this Rule, $1 will points to "app" string, not the section comes after forward slash.

Change that line as below, and let me know what happens please:

RewriteRule ^app/(.*)$ app.php/$1 [NC,L]

UPDATE:
Here's a sample of .htaccess file, according to CI Docs, I didn't try this, please let me know how it works:

RewriteEngine on

RewriteCond $1 !^(index\.php|app\.php|public|app)
RewriteRule ^(.*)$ index.php/$1 [NC,L]

RewriteCond $1 ^app/?
RewriteRule ^(.*)$ app.php/$1 [NC,L]


回答2:

I had this problem when I was doing a website with an admin panel and a front end area. Try using the standard removing the index.php method and use the routes function of codeigniter instead.

http://ellislab.com/codeigniter/user-guide/general/routing.html

Goodluck!



回答3:

you can route to app | site in other way

//index.php
if(preg_match('/^app/ui', $_SERVER['REQUEST_URI'])) {
     require_onse "app.php";
} else {
     require_onse "site.php"
}


回答4:

Okay, figured it out through restructuring my architecture.

Here it is:

app
| - (application name for backend) folder
| -- models, config, controllers, etc. folders
| - index.php for backend
site
| - models, config, controllers, etc.
system
| - CI system folder
index.php for the frontend (which is on 'site')

Turns out it looks like we don't need messy arm-wrestling with .htaccess for this to work. This just made myself look really dumb.

Thank you everyone for your help!