简体   繁体   中英

Mod Rewrite with multiple values

I'm trying to do something like this...Not sure where I am going wrong.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)^/(.*)^/(.*)$ /$1/$2.php?page=$3 [L,QSA]

Basically I want to be able to put in something like /item1/item2/item3 and have those values in $1, $2, and $3 etc....

Thanks

Instead of .* , try using .+ . Also be sure to consider there may be a trailing slash:

RewriteRule ^/(.+)/(.+)/(.+)/$ /$1/$2.php?page=$3 [l,qsa]
RewriteRule ^/(.+)/(.+)/(.+)$ /$1/$2.php?page=$3 [l,qsa]

Use following code in your .htaccess file:

Options +FollowSymlinks -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ /$1/$2.php?page=$3 [L,QSA,NE]

When a substitution occurs for a new URL, this module has to re-inject the URL into the server processing. To be able to do this it needs to know what the corresponding URL-prefix or URL-base is. By default this prefix is the corresponding filepath itself. But at most websites URLs are NOT directly related to physical filename paths, so this assumption will usually be wrong. There you have to use the RewriteBase directive to specify the correct URL-prefix.

http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html#RewriteBase

So maybe you have to set RewriteBase . For example you can set it to the root directory:

RewriteBase \

Also, you can't use more than one ^ in your pattern as it means 'start position of the string'.

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