#1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Dec 2007
    Location
    Tucson, AZ
    Posts
    592
    Rep Power
    79

    .htaccess mod_rewrite help


    Hey there!

    I'm trying to figure out how to write mod_rewrite rule that replaces "/ci/index.php/" with "/ci/" in the URL, but when /ci/ is loaded, it still loads the index.php file. I've done some Googling, but I can't find a comprehensive answer or guide. Is this even possible?

    This is what I've been trying. No luck:

    Code:
    RewriteEngine On
    RewriteRule ^(.*)/ci/(.*)/$ $1/ci/index.php/$2
    Thanks!

    EDIT - And yes, I've been looking through the .htaccess guide you guys have posted, but I'm still pretty confused... I'll keep looking though.
    Last edited by Dekudude; December 22nd, 2011 at 01:21 PM.
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,122
    Rep Power
    9398
    You need the generic "if not a file or a directory" RewriteConds. Then you can rewrite.
    Code:
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*)/ci/(.*)/$ $1/ci/index.php/$2 [L]
    If that gives you problems (specifically ones about rewriting limits or loops) then change the search pattern to
    Code:
    (.*)/ci/(?!index.php)(.*)/$
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Dec 2007
    Location
    Tucson, AZ
    Posts
    592
    Rep Power
    79
    Thanks for the help, but that does not seem to be doing anything at all. :/

    I am using CodeIgniter, if that helps. The code they recommend is:

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

    However, as my CodeIgniter directory is not the absolute root of the directory, I end up getting redirected to the top-level index file, not the one in my /ci/ directory.
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,122
    Rep Power
    9398
    What if you remove the leading slash and put that stuff in the /ci/.htaccess?
  8. #5
  9. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Add a RewriteBase directive to the file /ci/.htaccess
    Code:
    RewriteEngine On
    RewriteBase /ci/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Dec 2007
    Location
    Tucson, AZ
    Posts
    592
    Rep Power
    79
    Alright, great, I got it working. However, it only works with that RewriteBase being set to X.

    Is it possible to generalize the Rewrite base to work for "(anything)/ci"? Right now, given the location of the /ci folder, it only works if the path is "/sites/test/ci". I want to be able to use the same .htaccess file regardless of where the /ci directory is located. Is that achievable?
  12. #7
  13. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Not that I'm aware of.

    Comments on this post

    • Xyteran agrees : Shame. Thanks for your help, though!
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo