Welcome Guest, Not a member yet? Register   Sign In
.htaccess for multiple controllers
#1

[eluser]VirtualDevel[/eluser]
Hey guys, I have two main controllers, the site controller and the admin controller. I want to setup the htaccess file to remove the index.php from the url string. However, when I do it, it works fine for site but redirects to site for admin requests.

Request: http://localhost/ => go to controllers/main
Request: http://localhost/admin => go to controllers/admin
Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php/$1 [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 index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    #This last condition enables access to the images and css folders, and the robots.txt file
    #Submitted by Michael Radlmaier (mradlmaier)
    RewriteCond $1 !^(index\.php|images|javascripts|robots\.txt|css)
    RewriteRule ^(.*)$ index.php/$1 [L]
</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.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule>

config.php:
Code:
$config['index_page'] = "";
#2

[eluser]VirtualDevel[/eluser]
bump, anyone? I think I would need to add something to the routes or htaccess?
#3

[eluser]Pascal Kriete[/eluser]
Try adding a question mark behind the index.php in the two rewrite rules:
Code:
RewriteRule ^(.*)$ index.php?/$1 [L]

Let me know if that works.
#4

[eluser]Phil Sturgeon[/eluser]
Thats not the problem you are having. Is main a controller that you want all frontend url's to go through, or is it meant to be your default route?

If you just want / to go to controllers/main then set it in your config/routes.php to do so.

If you want all url's other than /admin to go to the controller/main, you will need to say so in your redirect rule, as it is currently JUST removing the index.php and not saying anything about main.

Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Send admin URL's to the admin controller (then skips the rest of the redirect rules)
    RewriteCond %{REQUEST_URI} ^/admin(.*)
    RewriteRule ^(.*)$ /index.php/admin/$1 [L]    

    # Redirects any request thats not a file or directory through to the main controller
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/main/$1 [L]

</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.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule>

This has not been tested as im at work, but should be pretty close.

I removed the systems.* condition as you should not be typing system into the URL - unless your index.php file is inside that folder (bad idea).
#5

[eluser]Pascal Kriete[/eluser]
@pyro I can see where you're coming from - I still believe his problem is much simpler as per the last statement:
Quote:it works fine for site but redirects to site for admin requests.

I guess we'll have to see what comes back Smile . Please let us know how it works out, VirtualDevel.
#6

[eluser]Phil Sturgeon[/eluser]
That last post I made was total tosh! I updated the rule to something mice nicer. Everything I removed from your htaccess was not being used and never would be.
#7

[eluser]VirtualDevel[/eluser]
Nope, still no go.. No matter what I do I cant seem to get routed to the admin controller. When I browse to domain.com/admin, the url says domain.com/admin but domain.com/index.php/main/index is executed, what I need is domain.com/index.php/admin. Funny enough, when I go to domain.com/index.php/admin this works. Thanks.

directory structure:
Quote:Controllers
- auth.php
- main.php
- admin
- main.php -> if session is not set redirect to auth.php
Thanks.
#8

[eluser]VirtualDevel[/eluser]
As a side not, I have tried creating an admin.php directly under the controllers folder and still nothing. Thanks, really appreciate the input!
#9

[eluser]VirtualDevel[/eluser]
Ok, weird, and this is probably silly of me. I tried it on the dev server and it works fine! However, on my person machine it fails, I'm using xampp for my web server and mysql. I'll try and upgrade to the latest versions and check the confs. Thanks a ton guys! Smile
#10

[eluser]VirtualDevel[/eluser]
HA! I'm an idiot, I had mod_rewrite commented out! Thanks a ton guys, really helped a lot..




Theme © iAndrew 2016 - Forum software by © MyBB