开发者

.htaccess - writing efficient codes using chain, next for url redirects

开发者 https://www.devze.com 2023-02-25 02:22 出处:网络
Suppose I have URLs as following on a user browsing page (www.date.com): www.date.com/Location-A/Education-B/Sex-C/Interest-D/

Suppose I have URLs as following on a user browsing page (www.date.com): www.date.com/Location-A/Education-B/Sex-C/Interest-D/

The situation is a user may browse only a number (1 or 2) of all (4) available criterias www.date.com/Education-B/ or www.date.com/Sex-C/Interest-E/

Also suppose the order of browsing criteria in the URL matter (i.e. A/B - ok, but B/A is not ok)

I had codes in my .htaccess file

RewriteEngine On

1 variable case

RewriteRule ^/Location-([^/]+)/$ /index.php?Location=$1 [L]

RewriteRule ^/Education-([^/]+)/$ /index.php?Education=$1 [L]

(...)

2 variables case

(...)

4 variables case

RewriteRule ^/Location-([^/]+)/Education-([^/]+)/Sex-([^/]+)/Interest-([^/]+)/$ /index.php?Location=$1&Education=$2&Sex=$3&Interest=$4 [L]

This will get stupidly long if I have 8 variables.

Is there smarter way of achieving above? Possibly using [C] - chain or [N] -开发者_如何学Go next? (the order of URL variables matter)

thanks in advance


Try this:

RewriteRule ^/([^-]+)-([^/]+)/$ /index.php?$1=$2 [L]
RewriteRule ^/([^-]+)-([^/]+)/([^-]+)-([^/]+)/$ /index.php?$1=$2&$3=$4 [L]
RewriteRule ^/([^-]+)-([^/]+)/([^-]+)-([^/]+)/([^-]+)-([^/]+)/$ /index.php?$1=$2&$3=$4&$5=$6 [L]
RewriteRule ^/([^-]+)-([^/]+)/([^-]+)-([^/]+)/([^-]+)-([^/]+)/([^-]+)-([^/]+)/$ /index.php?$1=$2&$3=$4&$5=$6&$7=$8 [L]
...
0

精彩评论

暂无评论...
验证码 换一张
取 消