CodeIgniter: Default controller

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    CodeIgniter: Default controller

    Hi all,
    I am trying to do something in CI which I cannot find any documentation for. I have set up my .htaccess so that if a file does not exist, and no controller/function is called it goes to the home page, but what I want is this:
    Instead of http://mysite.com/controller/function/variable
    I just use http://mysite.com/variable and it simply uses the default controller and index function.

    The reason is, I am creating a CMS where pages will be retrieved like:
    http://mysite.com/page/display/home
    But I would much prefer:
    http://mysite.com/home
    http://mysite.com/contact
    http://mysite.com/about

    Anyone done this or know how to?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    basicly something like this
    Code:
    RewriteRule ^([a-z])$ page/display/$1
    you might add your personal constraints, though.

    Comment

    • TheServant
      Recognized Expert Top Contributor
      • Feb 2008
      • 1168

      #3
      I have been trying to do something like that, however my mod rewrite skills are a bit behind. I have this:
      Code:
      RewriteEngine On
      RewriteRule ^(application) - [F,L]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ index.php/page/index/$0 [PT, L]
      Just checks if the file/directory exists, if not, then it should redirect to mysite.com/index.php/page/index/page_requested but it doesn't and I get a 500 error. Any help?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        don’t know too much mod_rewrite myself…

        a) check the syntax (manual)

        b) check if you are allowed to use mod_rewrite. wrap the code in
        Code:
        <IfModule mod_rewrite.c>
        </IfModule>
        c) use a RewriteBase

        d) (has nothing to do with the error) check for a / in the variable string (i.e. use a different RegEx)

        Comment

        • TheServant
          Recognized Expert Top Contributor
          • Feb 2008
          • 1168

          #5
          Well I don't know how to do this. I have realized that my removal of 'index.php/' with the following in my htaccess is not working:
          Code:
          RewriteEngine On
          RewriteRule ^(application|system) - [F,L]
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule (.*) index.php/$0 [PT,L]
          Yes I have already changed my config file so that $config['index_page'] = "";

          Anyone solved a similar problem?

          Comment

          • TheServant
            Recognized Expert Top Contributor
            • Feb 2008
            • 1168

            #6
            SOLVED!!! I changed this in my config:
            Code:
            $config['uri_protocol']	= "AUTO";
            ~to~
            Code:
            $config['uri_protocol']	= "ORIG_PATH_INFO";
            Don't know what it does, but it works!


            ------------------------------------------------------
            Edit:
            I have just changed it back to AUTO as I found it stuffs up the search engine friendly URL's, and found another solution. htaccess was changed to include a "?" like so:
            Code:
            RewriteRule (.*) index.php?/$0 [PT,L]

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              CI has this functionality built in: routing. In your config/routes.php file, try some thing like this:
              Code:
              $route['(a-z+)/?'] = "page/display/$1";

              Comment

              • TheServant
                Recognized Expert Top Contributor
                • Feb 2008
                • 1168

                #8
                Ha! Nice work Markus, I was trying to get that to work before with no luck, but tried a little harder and voila!

                I have another small problem though... Now none of my error pages work:
                The page you requested, 403_shtml, could not be found. <-- This is my error display message.
                I have realised that if I run everything through my other function, these pages won't work. Any ideas?

                Comment

                Working...