0

What I would like to do is to create a repeating pattern inside a rewriterule. For example:

RewriteRule ^([\w]+)/([\w]+)/(([\w]+)/([\w]+))*/?$ index.php?p=$1&s=$2(&$4=$5)*


Breaking it down:

On every page my ?p=BLA refers to a certain page, and every &s=BLA refers to a certain subpage. The rewriterule for this would obviously be:

RewriteRule ^([\w]+)/([\w]+)/?$ index.php?p=$1&s=$2

But now I want to add custom $_GET data to my url. For example I want to add &search=SEARCH, and I want to add &page=15, and anotherquery=BLA. The rewriterule I just wrote does not support this.

Is there any way to support an unspecified number of arguments in a rewriterule? For example by creating a repeating pattern? Because I don't know how many new arguments I want to add to the URL in advance.

3
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by cwarn23
1

Well it is possible to do if statements in regex like the following:

RewriteRule ^/?([\w]+)/(([\w]+)/(([\w]+)/(([\w]+)/(([\w]+)/(([\w]+)/(([\w]+)/|)|)|)|)|)|)$ index.php?a=$1&b=$3&c=$5&d=$7&e=$9&f=$11&g=$13

And in that regex up the 7 forward slashes may be used with [\w]+ between them. It will dynamically match from 1 forward slash to 7 foward slashes. But keep in mind for it to work you will need it to match [\w]+.

Edited by cwarn23: n/a

1

I have ran into this problem and I solve it by using php to handle this. Pretty much you send the entire url to a php page in a url variable.

Ex.

RewriteRule ^(.*)$ your_page.php?_url_=$1

Then with php you get the data and parse it yourself.

if ( isset( $_GET['_url_'] ) ) {
  //parse url
}

Worked great and still does in my custom mvc framework.

Maybe not the solution you are wanting, but will give you the flexibility you require.

Edited by somedude3488: n/a

0

Well it is possible to do if statements in regex like the following:

RewriteRule ^/?([\w]+)/(([\w]+)/(([\w]+)/(([\w]+)/(([\w]+)/(([\w]+)/(([\w]+)/|)|)|)|)|)|)$ index.php?a=$1&b=$3&c=$5&d=$7&e=$9&f=$11&g=$13

And in that regex up the 7 forward slashes may be used with [\w]+ between them. It will dynamically match from 1 forward slash to 7 foward slashes. But keep in mind for it to work you will need it to match [\w]+.

Thanks for the answer, that's definitely good to know. However, I have never seen a | being used inside a regex (in that place, I have of course seen it being used inside a pattern with the meaning this "or" that). What's the use of it behind a section?

0

I have ran into this problem and I solve it by using php to handle this. Pretty much you send the entire url to a php page in a url variable.

Ex.

RewriteRule ^(.*)$ your_page.php?_url_=$1

Then with php you get the data and parse it yourself.

if ( isset( $_GET['_url_'] ) ) {
  //parse url
}

Worked great and still does in my custom mvc framework.

Maybe not the solution you are wanting, but will give you the flexibility you require.

Very inventive. Might give it a try :). Thanks!

0

Thanks for the answer, that's definitely good to know. However, I have never seen a | being used inside a regex (in that place, I have of course seen it being used inside a pattern with the meaning this "or" that). What's the use of it behind a section?

Yes in regex it also acts as an "or" statement so you can have multiple regex patterns between the "or" statements.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.