1

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.

Kane Mitchell
  • 370
  • 2
  • 4
  • 12
  • You could do this trough php, parse url: examples here: [ http://php.net/manual/en/function.parse-url.php ] – Denis Solakovic Sep 16 '16 at 09:11
  • Hi @DenisSolakovic thanks for the input. Doing could work but it works better when variables are sent in the original format (?var_one= first). Unless I'm missing something. – Kane Mitchell Sep 16 '16 at 09:26
  • Hmm, you could probably do it with .htaccess [http://stackoverflow.com/questions/7827779/htaccess-url-rewrite-with-variables] , but my other suggestion is to explode url parameter with delimiter / and fetch your url fragments... – Denis Solakovic Sep 16 '16 at 09:33

1 Answers1

0

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]
ban17
  • 634
  • 8
  • 12