Friendly URL for MVC framework

Discuss practical ways rearrange URLs using mod_rewrite.

Postby richardk » Tue Feb 17, 2009 2:18 am

What does your error log say?

Apache 1.3 has an older (or less complete) regular expression library. It might not understand (?:optional)?. Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/(css|images|javascript)(/.*)?$ [NC]
RewriteRule ^([a-z0-9_]+)(/([a-z0-9_]+)(/([a-z0-9_]+))?)?/?$ ./$1.php?action=$3&id=$5 [NC,QSA,L]


Also try removing the Options line.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby phynv » Tue Feb 17, 2009 2:55 am

Thanks for your response. Unfortunately I have no access to the error logs. It is a server of a client who only has limited access on his own server :(

Removing the Options line didn't make things better; but I could verify while researching the web that apparently apache 1.x does not support non-referencing groups - as you already pointed out.

However, I tried your suggestion but without success. The rule seems not to be recognized at all.

UPDATE:
just to let you know: with your suggested rule http://www.domain.tld/controller.php?ac ... tion&id=id is working;
http://www.domain.tld/controller/action/id results in a 404
phynv
 
Posts: 8
Joined: Wed Feb 04, 2009 6:11 am

Postby richardk » Tue Feb 17, 2009 3:04 am

Try this mod_rewrite test.

And then
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/(css|images|javascript)(/.*)?$ [NC]
RewriteRule google http://www.google.com/? [NC,R,L]


And then
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/(css|images|javascript)(/.*)?$ [NC]
RewriteRule ^([^/]+)(/([^/]+)(/([^/]+))?)?/?$ /$1.php?action=$3&id=$5 [NC,QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby phynv » Tue Feb 17, 2009 3:36 am

Hi,

tried as you said.
This mod_rewrite test works.

Your first code works too.

Your second code gives a 500 internal server error...

Pretty weird :(
phynv
 
Posts: 8
Joined: Wed Feb 04, 2009 6:11 am

Postby richardk » Tue Feb 17, 2009 5:26 am

Doh. It matches the sub request to the .php file and loops.

You could check that the controller.php file exists
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/(css|images|javascript)(/.*)?$ [NC]
# Check /controller.php exists.
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)(/([^/]+)(/([^/]+))?)?/?$ /$1.php?action=$3&id=$5 [NC,QSA,L]

Or ignore all existing files and directories
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/(css|images|javascript)(/.*)?$ [NC]
# Ignore all requests to existing files.
RewriteCond %{SCRIPT_FILENAME} !-f
# Ignore all requests to existing directories.
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)(/([^/]+)(/([^/]+))?)?/?$ /$1.php?action=$3&id=$5 [NC,QSA,L]

Or ignore all sub requests
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Ignore all sub requests.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} !^/(css|images|javascript)(/.*)?$ [NC]
RewriteRule ^([^/]+)(/([^/]+)(/([^/]+))?)?/?$ /$1.php?action=$3&id=$5 [NC,QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Previous

Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 30 guests

cron