0

i'm trying to rewrite my urls in the form of

localhost/myapp/view/something or 
localhost/myapp/crud/something

to

localhost/myapp/view.pl?view=something or
localhost/myapp/crud.pl?action=something

all other request should be mapped like this: localhost/myapp/login to localhost/myapp/login.pl

I tried it with this .htaccess file but i get a recursion if i enable the last RewriteRule, i don't get why because i protected it with a RewriteCond. This is my first .htacces mod_rewrite file so please be patient with me ;)

RewriteEngine On

RewriteRule ^/?crud/([^/]*)$    /myapp/crud.pl?action=$2 [QSA]

RewriteRule ^/?view/([^/]*)$    /myapp/view.pl?view=$2 [QSA]

RewriteCond $1                     !^/(view|crud)$
RewriteRule ^/?([^/]*)$            /myapp/$1.pl [QSA]

Update:

I removed the leading / and added [L] flags now i get the following error in apache's error.log:

[Mon Jul 09 11:55:33 2012] [error] [client 127.0.0.1] File does not exist: /srv/http/myapp/myapp

New File:

RewriteRule ^crud/([^/]*)$    myapp/crud.pl?action=$2 [L,QSA]
RewriteRule ^view/([^/]*)$    myapp/view.pl?action=$2 [L,QSA]

RewriteCond $1                !^(view|crud)$
RewriteRule ^([^/]*)$         myapp/$1.pl [QSA]
zbenjamin
  • 65
  • 1
  • 1
  • 7
  • check out mod_rewrite flags - http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriteflags maybe the [L] will help you. – hovanessyan Jul 09 '12 at 09:27
  • This is an impressive firest attempt. :-) The leading `/?` are not required for `.htaccess` rules. Ditto loose the leading `/` on the replacements. (Read up on "Per Directory" syntax). Likewise the cond regexp should be `!(view|crud)`. Plus add the [L] flags. – TerryE Jul 09 '12 at 09:38
  • Ok i got rid of the leading / and added the [L] flags, now i get this in error_log: /srv/http/myapp/myapp – zbenjamin Jul 09 '12 at 10:01

1 Answers1

1

Since your URIs contain myapp, I am assuming that this is in DOCroot/myapp/.htaccess, in which case your base is /myapp/ and all rewrites are relative to this. (Look for "Per Directory" in the documentation.)

RewriteEngine On
RewriteBase   /myapp/

RewriteRule ^crud/([^/]*)$    crud.pl?action=$2 [L,QSA]
RewriteRule ^view/([^/]*)$    view.pl?action=$2 [L,QSA]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(\w+)$            $1.pl             [L,QSA,NS]

I've also changed the last rule to rewrite UTIs for /myapp/xxxxx (any word) to /myapp/xxxxx.pl as long as it doesn't map to a file or directory name.

Also look at Tips for debugging .htaccess rewrite rules, and in you case since you have access to the Apache config, consider enabling RewriteLog and RewriteLogLevel in your system config (or LogLevel alert rewrite:trace3 if you are using Apache 2.4).

Community
  • 1
  • 1
TerryE
  • 10,724
  • 5
  • 26
  • 48
  • can i choose every prefix as RewriteBase? For example rewrite /app to /myapp ? – zbenjamin Jul 09 '12 at 13:37
  • Read the doc'n on RewriteBase. You should only have 1 per .htaccess file giving the base URI which gets to this directory. Also go through that debugging tips Q that I linked to, which explains when and which .htaccess files are executed. You would normally rewrite /app to /myapp in the `DOCroot/.htaccess` file. – TerryE Jul 09 '12 at 13:44