Simple mod_rewrite question

I know this can be done, but I can’t find the right sample on the web at the moment, so I though I’d ask you guys.

I need to translate mvc style routes to an url. What I have is this…

<IfModule mod_rewrite.c>
	RewriteEngine on
	RewriteRule ^/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/(.*)$ index.php?controller=$1&action=$2&id=$3 [QSA,L]
	RewriteRule ^/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?controller=$1&action=$2 [QSA,L]
	RewriteRule ^/([a-zA-Z0-9_-]+)$ index.php?controller=$1 [QSA,L]
</IfModule>

But it only works if the url contains index.php (i.e. index.php/card/edit/2 works, but I just want /card/edit/2). What do I change in the above to make it work? Thanks!

Serena,

First, you’ve “earned” a Standard Rant:

[rant #4][indent]The definition of an idiot is someone who repeatedly does the same thing expecting a different result. Asking Apache to confirm the existence of ANY module with an <IfModule> … </IfModule> wrapper is the same thing in the webmaster world. DON’T BE AN IDIOT! If you don’t know whether a module is enabled, run the test ONCE then REMOVE the wrapper as it is EXTREMELY wasteful of Apache’s resources (and should NEVER be allowed on a shared server).[/indent][/rant 4]

Second, your use of ^/ in the RewriteRules is specific to Apache 1.x. Because Apache 2.x will not match the leading /, you need to:

a. Confirm that you’re using one of the few remaining Apache 1.x installations OR

b. Change ^/ to ^/? so that it’s available to both versions of Apache OR

c. Change ^/ to ^ (specific to Apache 2.x)

Other than those “minor” defects, you’ve done very well with your mod_rewrite coding … and their ORDER! That’s particularly rare so :tup:

[COLOR="#FF0000"]<IfModule mod_rewrite.c>[/COLOR]
	RewriteEngine on
	RewriteRule ^[COLOR="#FF0000"]/[/COLOR]([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/(.*)$ index.php?controller=$1&action=$2&id=$3 [QSA,L]
	RewriteRule ^[COLOR="#FF0000"]/[/COLOR]([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?controller=$1&action=$2 [QSA,L]
	RewriteRule ^[COLOR="#FF0000"]/[/COLOR]([a-zA-Z0-9_-]+)$ index.php?controller=$1 [QSA,L]
[COLOR="#FF0000"]</IfModule>[/COLOR]

Removing the / will correct that as I believe that has disabled every rule you have.

Regards,

DK

Well it’s apache2 actually. And I got that code off the inet. I have no clue how to author them as I don’t understand regex (never have in all of 30+ years coding), so I probably never will. Does the code change for version 2.0?

P.S. The .htaccess file now looks like this:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/(.*)$ index.php?controller=$1&action=$2&id=$3 [QSA,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?controller=$1&action=$2 [QSA,L]
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?controller=$1 [QSA,L]
:and now it breaks the whole site with a 500 error.

Any more advice?

SR,

Good! Apache 2 does a much better job than Apache 1.

:nono: IMHO, a webmaster must NEVER use code he/she does not understand. Okay, that’s just a personal (professional) thing with me.

There is NOTHING wrong with the mod_rewrite code you have:

RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/(.*)$ index.php?controller=$1&action=$2&id=$3 [QSA,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?controller=$1&action=$2 [QSA,L]
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?controller=$1 [QSA,L]

Therefore, I must ask for the ENTIRE .htaccess contents so determine where the syntax error is because that’s the normal cause of 500’s.

If you elect to text your .htaccess file (a good thing to do), comment out every line and enable one at a time until you find the offending line, investigate and correct it’s syntax, then continue to the end.

Regards,

DK

Thanks. I figured it out. There was no .load file for it. I didn’t know they’d changed how they handled it since apache1. Live and learn. The reason it wasn’t tripping initially was actually because of the IfModule line, which, as somebody who can edit the entire server, I don’t really need anyway. I stripped that from the code, created the .load file, and all is well.