简体   繁体   中英

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

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 (ie 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] - 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]
...

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