• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Strange (to me) routing behaviour

#1
Hi All,

I was hoping to get some clarification about routing.  I was under the impression that the only pages you could access were ones that were setup in the routes.php file.  Example:

URL: abc.com

Route entry: $route['hello'] = 'example/hi';

As expected, I can access the page at abc.com/hello.

Now, if i comment out that line in the code:

//$route['hello'] = 'example/hi';

I get a 404 if i try accessing abc.com/hello (which again, I expect).  What I find peculiar though is that if I enter abc.com/example/hi in my browser, I can access the content.  I didn't know that such a thing was possible.  Could someone offer me some insight (even if it's just as simple as  yes... it is possible)?

Not sure if it makes a difference but here is my .htaccess file:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Thanks.
Reply

#2
try this
Code:
<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /hi

RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

</IfModule>

<IfModule !mod_rewrite.c>
   ErrorDocument 404 /index.php
</IfModule>
www.arthnugraha.com
Learning by doing
Reply

#3
There's nothing wrong with your .htaccess file, it's your assumption about how routing in CodeIgniter works was wrong ... I don't know where you got that from, it's all explained in the manual:

https://ellislab.com/codeigniter/user-gu...uting.html
Reply

#4
(12-04-2014, 09:22 PM)lazylazz Wrote: I get a 404 if i try accessing abc.com/hello (which again, I expect).  What I find peculiar though is that if I enter abc.com/example/hi in my browser, I can access the content.  I didn't know that such a thing was possible.  Could someone offer me some insight (even if it's just as simple as  yes... it is possible)?


/hello is not an actual controller, so your route setting told it to use example/hi (controller/method)

Codeigniter by default however will look for a controller with same name

/example/hi codeigniter will go to your example controller and run your hi method because it exists.


(Some frameworks require you set all the routes, Codeigniter will auto match to controller files by name. )
Reply

#5
Ok, the good and the bad (for me anyway)
1) the CI ".htaccess" script in the user guide does in NO WAY shape or form work (with MY setup)
I have win8.1, wamp 2.5

To start, Followed tutorial. ALWAYS copy and pasted to avoid typo's. Tutorial works fine except for "view article" links but still have one more page to go..maybe the fix is on that page?

Ran <?php phpinfo(); ?> to verify that removing the # from the mod_rewrite Loadmodule was successful. Apache set to www so that's good

What wamp does - Opens localhost as usual with default page. Click on project...reopens default page (w/o wamp icon) Keep clicking - same result.

Use modified .htaccess from above post. Functionality restored WTF? BUUUT...still have to use index.php/news or whatever.

Would anyone like to enlighten me on why the *bleep* I just put myself thru all of that for absolutely NO result???? This is why I threw the towel in on Cake.

I swear to F'ing christ if I can just get rid of the index.php I will NEVER in my *bleeping* life EVER....EVER screw with mod_rewrite again. You have my word on it!

Sorry for frustration but, I've googled the crap out of it and nothing. I just can't win with mvc frameworks, perhaps I should just hard code a minimalist backend to get the mysql functionality I desire since I can't find it anywhere else Sad

This learning experiment has not been fun....
Reply

#6
Try this one, it's the one I use and always has worked for me.

PHP Code:
<IfModule mod_rewrite.c>

 
   # Make sure directory listing is disabled
    
Options +FollowSymLinks -Indexes
    RewriteEngine on

    
# NOTICE: If you get a 404 play with combinations of the following commented out lines
    #AllowOverride All
    #RewriteBase /wherever/ci/is
    
RewriteBase /

    
# Restrict your site to only one domain
    # Important USE ONLY ONE OF THESE OPTIONS BELOW!

    # Option 1: To rewrite "www.domain.com -> domain.com" uncomment the following lines.
    #RewriteCond %{HTTPS} !=on
    #RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    #RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

    # Option 2: To rewrite "domain.com -> www.domain.com" uncomment the following lines.
    #RewriteCond %{HTTPS} !=on
    #RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
    #RewriteCond %{HTTP_HOST} (.+)$ [NC]
    #RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

    # Option 3: Remove index.php from URL
    
RewriteCond %{HTTP:X-Requested-With}    !^XMLHttpRequest$
    
RewriteCond %{THE_REQUEST}                ^[^/]*/index\.php [NC]
    
RewriteRule ^index\.php(.*)$            $[R=301,NS,L]

    
# Send request via index.php (again, not if its a real file or folder)
    
RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond 
%{REQUEST_FILENAME} !-d

    RewriteCond 
$^(robots\.txt|favicon\.ico|style\.css)

    
# deal with php5-cgi first
    
<IfModule mod_fcgid.c>
        
RewriteRule ^(.*)$ index.php?/$[QSA,L]
    </
IfModule>

    <
IfModule !mod_fcgid.c>

        
# for normal Apache installations
        
<IfModule mod_php5.c>
            
RewriteRule ^(.*)$ index.php/$[QSA,L]
        </
IfModule>

        
# for Apache FCGI installations
        
<IfModule !mod_php5.c>
            
RewriteRule ^(.*)$ index.php?/$[QSA,L]
        </
IfModule>

    </
IfModule>

</
IfModule
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.