Mod Rewrite issues

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Mod Rewrite issues

Post by John Cartwright »

Okay first and foremost, this is probably the weirdest thing I've ever encountered. I don't know why, it just is :wink: Serious thought..

Okay, I am running the latest version of Ubuntu, with Apache 2.0, and yes mod_rewrite module is loaded and works fine for simple specific rules. Now!, heres my problem. I'm trying to route ALL requests to my front controller,

Code: Select all

RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1
Just ignore those Cond's for now, I know they arn't causing the problem. Now lets say I visit http://localhost/index/index or for that matter anything that starts with /index/, everything is working like a charm. For debugging, I simply put a exit('here'); to see if the request was made correctly.. which it was. Now, if I visit anything else, such as http://localhost/index2/ or ihttp://localhost/idontwork/ I am simply getting a 404..

I am not ruling out a server misconfiguration here, so if anyone can shed some light on this disastrous rule.. please do.

Thank you.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

This rule

Code: Select all

RewriteRule ^(.*)$ index.php/$1
is saying that anything that comes into your domain should be rewritten to index.php/<whatever-was-entered-into-the-url>. Is that what you want? And have you tried throwing some flags out there, like [QSA,L]?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Everah wrote:s that what you want?
Yes.
And have you tried throwing some flags out there, like [QSA,L]?
Yes, I have just gone through the works.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Try adding a slash.

Code: Select all

RewriteRule ^(.*)$ /index.php/$1 [QSA,L]
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

If you're just adding everything as an argument to your front controller, why bother? Isn't the original still accessible via $_SERVER['REQUEST_URI'] anyway?

Cheers,
Kieran
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I'm going to just recompile Apache with 1.3.. gave up horribly.
Kieran Huggins wrote:If you're just adding everything as an argument to your front controller, why bother? Isn't the original still accessible via $_SERVER['REQUEST_URI'] anyway?

Cheers,
Kieran
That is the point, to direct all requests to a focal point in your application. I don't really understand what your getting at? Why bother with what?


Edit | back to the drawing board, going to have to recompile with Apache 2 again, forgot my new hosts is running Apache2 and want to avoid incompatibilities such as this.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I think he means you can do this instead:

Code: Select all

RewriteEngine On
RewriteRule .* index.php
And then access the information you're adding after index.php/ with $_SERVER['REQUEST_URI'] instead
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

sorry - I was a bit unclear.

What I think you're doing is redirecting every URL to your front controller at /index.php - and you're having trouble passing the original URL to it as an argument.

My suggestion is to not pass the original URL at all, but rather simply reference it in your script from the $_SERVER['REQUEST_URI'] variable. Even after a mod-rewrite, $_SERVER['REQUEST_URI'] retains the original, unmodified URL.

Cheers,
Kieran
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

The Ninja Space Goat wrote:I think he means you can do this instead:

Code: Select all

RewriteEngine On
RewriteRule .* index.php
And then access the information you're adding after index.php/ with $_SERVER['REQUEST_URI'] instead
Yup - that's what I mean exactly!

Wow... I seem to be having trouble with that thing where you type the words in to the other thing and still be clear about the original thing (of which I'm speaking). 8)

This is why I sucked at French... I have enough trouble with English :oops:
Post Reply