Link to home
Start Free TrialLog in
Avatar of Terry Woods
Terry WoodsFlag for New Zealand

asked on

Apache mod_rewrite for removing index.php not working for controller functions

My page loads my controllers default function with URL:
mydomain.com/dev/website_factory/
and it loads the create_website function from:
mydomain.com/dev/website_factory/index.php/myfactorywebsites/create_website

But when I try to load:
mydomain.com/dev/website_factory/myfactorywebsites/create_website
I get a Not Found (404) error

I was hoping my htaccess file would make that work, but apparently it doesn't. Any idea as to what's wrong?

My htaccess file contains:
RewriteEngine on
RewriteBase /dev/website_factory
RewriteCond $1 !^(index\.php|images|robots\.txt|css|js)
RewriteRule ^(.*)$ index.php/$1 [L]

Open in new window

Avatar of hielo
hielo
Flag of Wallis and Futuna image

Refer to:
http://www.bloghash.com/2006/12/apache-mod_rewrite-examples/

Try:

RewriteEngine on
RewriteBase /dev/website_factory

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?factory=$1&method=$2  [QSA,NC,L]
RewriteRule ^([^/\.]+)/?$ /index.php?factory=$1 [QSA,NC,L]

Open in new window

Avatar of Terry Woods

ASKER

Still trying to resolve this.

It appears not much is allowed to be done from the htaccess.txt file with the set up we've got, so I stuck my rewrite directly into the apache configuration. Now the mod_rewrite appears to work (at least, without it, I get a 404 error), but now I'm getting a 403 Forbidden error, which is odd.

The $1 for my existing RewriteCond doesn't seem correct; the documentation seems to indicate that would refer to the captured group from a previous RewriteCond, which I don't have.

What does the:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

Open in new window

part of your solution do? I'd guess it checks to ensure the request doesn't match a directory or file. I think perhaps I actually want:
RewriteCond %{REQUEST_URI} !^(index\.php|images|robots\.txt|css|js)

Open in new window


Am currently trying to figure out why I'm getting the 403 error. There's no htaccess file in my website_factory folder, and I still get a 403 error trying to access that. There is an existing index.php file, so it's not just trying to do a directory listing (which is disallowed). Other sites on the webserver are still working.
I get the 403 error as soon I try to turn on the rewriteengine. I think it's a higher level server setting causing that, so I've enquired with the team who manages that.

In the meantime, there's the outstanding question in my previous comment, about your code, that you might be able to help with...
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is mostly working:
  RewriteEngine on
  RewriteBase /dev/website_factory

  #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} !-d
  RewriteCond %{REQUEST_FILENAME} !-f

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

Open in new window


I will post the outstanding problem as a new question... thanks for your help!