简体   繁体   中英

mod_rewrite, how to rewrite links correctly

I am trying to rewrite links on my site. Possible links before rewrite and what I want to see after rewrite:
/index.pl?mode=users&action=add - /users/add/
/index.pl?mode=streets&action=edit&id=7 - /streets/edit/7
/index.pl?mode=users&action=info&id=7 - /users/info/7
etc

.htaccess content:

RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(jpe?g|gif|bmp|png|tiff|css|js)$ [NC]
RewriteRule ^([A-Za-z0-9-]+)/((edit|delete|info))/([0-9]+)$ index.pl?mode=$1&action=$2&id=$3 [L]
RewriteRule ^([A-Za-z0-9-]+)/((add))/?$ index.pl?mode=$1&action=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)/?$ index.pl?mode=$1 [L] 

/users/add/ or /street/add/ are working properly, but...

Problems:
/users/edit/xx - I can't accept ID in perl script. Why?
/users/info/xx - I can't even get to info section /?mode=users&action=info&id=7 page (it have to show blank table with wrong ID

Btw... My site has 'switch' structure. I mean if mode is users, then it load "users.pl", if mode=streets loading "streets.pl", etc. About 2nd problem - sure I have info section at users.pl! And link /?mode=users&action=info&id=7 work perfect.

ps: added php-tag because it is not 'perl' problem , but php is equal to perl, so php-followers can help me too
pps: sry for my not very good english.

You have too many parentheses, which makes your backreferences off-by-one:

RewriteRule ^([A-Za-z0-9-]+)/(edit|delete|info)/([0-9]+)$ index.pl?mode=$1&action=$2&id=$3 [L]
RewriteRule ^([A-Za-z0-9-]+)/(add)/?$ index.pl?mode=$1&action=$2 [L]

Those two rules had too many parentheses around the ((edit|delete|info)) and ((add)) (though the "add" isn't affected because $2 correctly backreferences it).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM