1

I'd like to create a RewriteRule to get each part of a pathname URL.

Here is a sample example:

Let's say I call this URL :

127.0.0.1/service/param1/param2/param3/param4/param5/param6

I would like to transform this call by this one :

127.0.0.1/index.php?s=service&p_1=param1&p_2=param2&p_3=param3&p_4=param4&p_5=param5&pa_6=param6

I tried a lot of different RegEx but whithout success unfortunately.

Here is the one I was a lot confident about but it fails

RewriteRule ^(str)[/]?(.*)[/]?(.*)[/]?(.*)[/]?(.*)[/]?(.*)[/]?(.*)$ /index.php?s=$1&p_1=$2&p_2=$3&p_3=$4&p_4=$5&p_5=$6&p_6=$7 [L,NC,QSA]

Any help would be very appreciate !

Xor
  • 199
  • 1
  • 2
  • 8
  • Are these parameters optional? – Sumurai8 Dec 24 '16 at 16:04
  • 1
    Almost sure there is no such option, but you could easily do this with PHP. Just RewriteRule everything to the `index.php` file, and inside that php file do the splitting – Dekel Dec 24 '16 at 16:05
  • You have no `str` in your URL. `(service)[/]?(.*)[/]?(.*)[/]?(.*)[/]?(.*)[/]?(.*)[/]?(.*)$` should do it. The `/` also doesnt need to be in a character class and you probably want to require the `/`s and value between. e.g. `(service)/(.+?)/(.+?)/(.+?)/(.+?)/(.+?)/(.*)$`. – chris85 Dec 24 '16 at 16:06
  • @Sumurai8 Yes, there are. – Xor Dec 24 '16 at 16:21
  • @chris85 I'm going to try it out ! Thanks ! – Xor Dec 24 '16 at 16:21
  • @chris85 After try, I think your regex can work if parameters are not optional. but in my case, we should be able to call `127.0.0.1/service/param1` and also `127.0.0.1/service/param1/param2/param3` or `127.0.0.1/service` :-) – Xor Dec 24 '16 at 16:24
  • @Dekel Well, I have found this `^(service)(?:/([^/]+)(?:/([^/]+))?(?:/([^/]+))?)?/?$` which is working but only if I have less than 4 parameters. But I try to understand this regex without success ahah ! – Xor Dec 24 '16 at 16:26
  • If you know the number of parameters - it's ok. If you don't know the number - I don't think you will be able to do this with htaccess regex – Dekel Dec 24 '16 at 16:30
  • Why not do this with PHP? – Dekel Dec 24 '16 at 16:30
  • Well it was working with htaccess (following the regex I gave previously) that why I tried. And it's really simple to use $_REQUEST after. One thing important that I didn't ask about, is that I know the maximum of parameters that can be passed. In my case, I can have 6 parameters maximum. – Xor Dec 24 '16 at 16:34
  • Your going to write extra parameters if the groups are optional – chris85 Dec 24 '16 at 17:02

1 Answers1

2

Since your parameters are optional, you want to make the whole capture groups optional, not just your slashes. To accomplish this we can use non-capturing groups.

RewriteRule ^([^/]+)(?:/([^/]*))?(?:/([^/]*))?(?:/([^/]*))?(?:/([^/]*))?(?:/([^/]*))?(?:/([^/]*))?(?:/([^/]*))?$ /index.php?service=$1&p1=$2&p2=$3&p3=$4&p4=$5&p5=$6&p6=$7&p7=$8 [L]

([^/]+) captures the service. It is not optional, and should always be there. The [^/] part matches a character that is not a slash... and we match at least one, and as much as we can. Then we use groups of (?:/([^/]*))? to capture each argument. The ?: syntax marks a group as non-capturing. The contents will not be used for the $x replacement strings. The ? behind the whole non-capturing group makes it an optional argument. The inner capture group will be used for the replacement later on. All replacement strings are replaced by something. If nothing is matched for a particular replacement group, it is replaced by the empty string.

An url like http://example.com/service/param1/param2/param3/ will result in an url as http://example.com/index.php?service=service&p1=param1&p2=param2&p3=param3&p4=&p5=&p6=&p7=.

If you want to match more than 8 optional parameters, you are better off passing the request on to a php router. If you have key-value pairs as path segments, this answer may help you.

Community
  • 1
  • 1
Sumurai8
  • 20,333
  • 11
  • 66
  • 100