Jump to content

htaccess woes


Recommended Posts

I am working with the CodeIgniter framework to build my site. Well recently I have been converting my admin section over to using the CI framework.

 

I introduced an admin directory in my controllers folder and now cannot access anything inside that folder without going to myurl.com/index.php?/admin/controller_name

 

Here is my .htaccess file

 

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #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
    RewriteRule ^(.*)$ index.php?/welcome/$1 [L]

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^(.*)$ index.php?/admin/$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>

 

Thanks in advance for any help!

Link to comment
Share on other sites

Did you solve this problem? I see it's about a week old. Looking at your code there are two potential issues that I can see. The most obviously is probably that this code...

 

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

 

...will probably match your admin related URI's meaning you will never get passed this rule, thus whatever code you have used, it will never actually be ran. This can probably be fixed by simply moving the admin related rules above this 'catch all' segment.

 

If you still have issues with this then providing example URI's that you are testing would go a long way in helping us solve the problem. If you have solved the problem then if you could kindly post a quick description of the fix and click the 'Mark Solved' button located in the bottom left corner of threads you started.

Link to comment
Share on other sites

Thanks for your help!

 

I just tried your suggestion this morning and it was the fix I was looking for :)

 

   <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #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_URI} !-d
RewriteRule ^admin(.*)$ index.php?/admin/$1 [L]
   
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?/welcome/$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>

 

The question I have is, can you explain a particular line for me? I am still trying to understand writing htaccess files and am having a little bit of a hard time.

 

RewriteRule ^admin(.*)$ index.php?/admin/$1 [L]

 

Can you explain the above to me? I understand what it does all around but anyway you can break it down for me?

 

Thanks!

 

Link to comment
Share on other sites

Sure.

 

RewriteRule - Fairly self explanatory, we are telling the server we wish to use this directive.

^admin - Matches against the start of the REQUEST_URI, meaning it must begin with the string literal 'admin'.

(.*)$ - Captures all characters up until the end of the REQUEST_URI. The . character means any character, the * mean repeat the previous character 0 or more times (so in this case 0 or more of any character). By placing this inside a set of parentheses we are creating a capture group, i.e. saying to Apache, please remember this section of the string. The $ means match against the end of string, this just makes sure the entire REQUEST_URI is matched against.

index.php?/admin/ - This is the literal part of the URI that will actually be served up.

$1 - This tells Apache to append on the value from capture group 1, therefore it will place whatever is between the first set of parentheses in the first section of the directive. In this case it will basically be the whole REQUEST_URI minus the admin literal.

[L] - This is the Last flag, it tells Apache that if a URI is rewritten using this rule, it should not have any more RewriteRules applied to it.

 

Assuming that the URI's you wish to use are of the form http://domain.com/admin/page-name you might wish to change the pattern to...

 

RewriteRule ^admin/(.*)/?$ index.php?/admin/$1/ [L]

 

This will prevent the capture group grabbing that first forward slash thus preventing the target address being http://domain.com/index.php?/admin//page-name/. It also makes the trailing slash optional so that it will work with or without it. I placed the trailing slash after the $1 purely because I'm not sure if CI requires the trailing slash.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.