46568

URL rewrite for converting path to querystring

Question:

I try to make the URL like the following

http://domain/d1/d2/k1/val1/k2/val2/k3/val3

To be

http://domain/index.php?one=d1&two=d2&k1=val1&k2=val2&k3=val3

Above one and two are fixed keys, the rest of the path is for key-value pairs. When I have more key-value pairs (more than three) attached after /d1/d2/, how do I write out the URL rewrite rules?

Update#1:

Below is what I have so far. I cannot have a dynamic key value pairs appended at the end of the first two folders.

RewriteEngine on RewriteBase / RewriteCond %{REQUEST_URI} !\.(css|png|jpe?g|gif)$ [NC] RewriteCond %{REQUEST_URI} !index\.php [NC] RewriteRule ^([^/]+)/([^/]+) /index.php?one=$1&two=$2 [QSA,L]

I was thinking to add one more folder "query" (see below) to help for pattern match for key-value pairs because you know after query all the folders are about key-value pairs. But, I cannot go further.

http://domain/d1/d2/query/k1/val1/k2/val2/k3/val3

Answer1:

The trick is to loop through a rewrite rule until all elements of the directory are replaced. First change all the key/value pairs, then d1 and d2:

# Convert key/value pairs at end of line. Loop (N) until path is just two levels deep RewriteCond %{REQUEST_URI} ^/(\w+)/(\w+) RewriteRule (.+)/(\w+)/(\w+)$ $1?$2=$3 [QSA,N] # Replace two final paths to query string and append to /index.php RewriteCond %{REQUEST_URI} !^/index.php RewriteRule /(\w+)/(\w+) /index.php?one=$1&two=$2 [QSA]

Answer2:

An alternative would be to redirect all requests at /index.php with the original path appended as the query string. You could then easily examine the elements of the original path using PHP instead.

See <a href="https://stackoverflow.com/a/8019730/1554386" rel="nofollow">https://stackoverflow.com/a/8019730/1554386</a>

Recommend

  • dynamic change of templateUrl in ui-router from one state to another
  • Appending same node in different windows
  • Appending into another table and avoiding duplicates
  • Returning semi-unique values from a list
  • Android gcm notification payload vs data payload?
  • How to add System.Windows dll to Visual Studio 2010 express?
  • How to work with Master Page that is attached to the page via the page's basepage?
  • Fire callback when selection was made with select2 4.0, and retrieve the value of last selection
  • Refactoring advice: maps to POJOs
  • Regex for URL rewrite with optional query string parameters
  • How to validate a year I enter in textbox using jquery rule?
  • Laravel at least one field is required
  • Is there a package like bigmemory in R that can deal with large list objects?
  • Setting the run time properties on SpringApplicationBuilder()
  • jQuery: add elements until a particular height is reached
  • Is there some graphical way to create my own configuration file on SonarLint?
  • Apache RewriteRule redirection with url encoded
  • htaccess add www if not subdomain, if subdomain remove www
  • one Local Olampyad Questions on Informatic in 2011
  • Use of this Javascript
  • Get data from AJAX - How to
  • Why value captured by reference in lambda is broken? [duplicate]
  • Email format validation in mvc3 view
  • Adding a button at the bottom of a table view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • How to add date and time under each post in guestbook in google app engine
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • Symfony2: How to get request parameter
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • retrieve vertices with no linked edge in arangodb
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Understanding cpu registers
  • Why joiner is not used after Sequence generator or Update statergy
  • coudnt use logback because of log4j
  • Add sale price programmatically to product variations
  • Recursive/Hierarchical Query Using Postgres
  • UserPrincipal.Current returns apppool on IIS
  • Android Heatmap on canvas or ImageView
  • Conditional In-Line CSS for IE and Others?