简体   繁体   中英

.htaccess - Rewrite with 3 variables but after that get the rest of the URL

So I'm currently using my .htaccess to write my URL to be more URL friendly. But I'm having trouble getting the fourth variable that could be more folders. I'll try and explain it as best I can below.

So at the moment I have the following URL working:

www.mysite.co.uk/first/second/third

Now for this I am using the rewrite rule:

RewriteRule ^e/([a-zA-Z0-9-/]*)/([a-zA-Z0-9-/]*)/([a-zA-Z0-9\-\/]*)$ index.php?var_1=$1&var_2=$2&var_3=$3   [L]

My problem comes when I one of the following happens:

www.mysite.co.uk/first/second/third/fourth

www.mysite.co.uk/first/second/third/fourth/fifth

www.mysite.co.uk/first/second/third/fourth/fifth/sixth

From the above I want the following variables to be returned:

$_GET['var_1'] = first

$_GET['var_2'] = second

$_GET['var_3'] = third/fourth

OR

$_GET['var_3'] = third/fourth/fifth

OR

$_GET['var_3'] = third/fourth/fifth/sixth

So basically I always have a 'var_1' and 'var_2' set but the third variable can be the rest of the URL.

I hope this makes sense and that it's possible.

I'm not sure why you your rule starts with ^e/ if your urls will be in format : www.mysite.co.uk/ e /first/second/third/fourth then use this regex:

RewriteRule ^e/([a-zA-Z0-9-]+)(/([a-zA-Z0-9-]+))?(/([a-zA-Z0-9-/]*))?$ index.php?var_1=$1&var_2=$3&var_3=$5 [L]

If you need it without /e/ at start www.mysite.co.uk/e/first/second/third/fourth
then remove /e/ from start

RewriteRule ^([a-zA-Z0-9-]+)(/([a-zA-Z0-9-]+))?(/([a-zA-Z0-9-/]*))?$ index.php?var_1=$1&var_2=$3&var_3=$5 [L]

Example above will work even with url-s like /first and /first/second but if you don't need it and you want to have this rule just in case of 3 or more params then you can simplify rule:

RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-/]+)$ index.php?var_1=$1&var_2=$2&var_3=$3 [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