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.

Recommended Answers

All 5 Replies

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]+.

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.

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?

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!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.