Welcome Guest, Not a member yet? Register   Sign In
RewriteRule fails when index.php is in sub-directory
#1

[eluser]tarsusc81[/eluser]
I'm using the mod_rewrite rule to remove index.php from my URLs like so:

Code:
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

I'm now trying to move my entire CodeIgniter site to an existing web site for testing purposes only. To leave the existing site undisturbed, I want all CodeIgniter files to sit in a sub-directory of the site called "codeigniter." So in other words, my root CodeIgniter directory is at http://www.mysite.org/codeigniter/.

This should be perfectly possible as long as I adjust baseurl and all other relevant config settings (and I have). If fact, it should be one of CodeIgniter's great strengths.

But the one part I'm having trouble with is the rewrite rule. This rule in .htaccess in my site's root folder (not /codeigniter) should do what I need:

Code:
RewriteRule ^codeigniter/(.*)$ codeigniter/index.php/$1 [L]

That should rewrite a url like http://www.mysite.org/codeigniter/articles to http://www.mysite.org/codeigniter/index.php/articles. But I'm having all kinds of unexpected problems.

The rule causes an internal server error. If I remove the "/$1" at the end like this there is no error:

Code:
RewriteRule ^codeigniter/(.*)$ codeigniter/index.php [L]

Or if I keep the "/$1" but remove the sub-directory there is no error:

Code:
RewriteRule ^codeigniter/(.*)$ index.php/$1 [L]

But both together cause the error. (I must admit, I don't fully understand the URL scheme of index.php/someinfo in the first place; that was completely new to me when I got into CodeIgniter.)

Can another pair of eyes see what I'm doing wrong here?
#2

[eluser]tarsusc81[/eluser]
Just realized that I neglected the RewriteCond that excludes /codeigniter/index.php from the rule:

Code:
RewriteCond $1 !^(codeigniter/index\.php)

(Note I don't need any of the other exceptions like 'css', 'js', etc., because the rule will only operate on requests matching "^codeigniter" anyway.)

I thought for sure this would fix it. No dice. I still get an internal server error, and now I have to remove "/$1" from the rewrite rule and "codeigniter" from the RewriteCond for the error to go away. I'm stumped.
#3

[eluser]tarsusc81[/eluser]
Aha! I did not fully understand that the "$1" in the RewriteCond is actually a reference to the capturing group from the RewriteRule. (Since RewriteCond comes first, I didn't know the pattern match is actually the first thing to be processed.)

Therefore, I needed one capturing group in the regex pattern for the RewriteCond to operate on, and a second for the RewriteRule to reference.

This works:

Code:
RewriteCond $1 !^codeigniter/index\.php
RewriteRule ^(codeigniter/(.*))$ /codeigniter/index.php/$2 [L]

Bingo bango! Now my whole CodeIgniter site sits in a sub-directory, and all other URLs are none the wiser.

So, thanks for . . . listening, I guess! Maybe my learning process here will help someone else.




Theme © iAndrew 2016 - Forum software by © MyBB