0

I have following url

http://domain/phpfile.php/something1-param1/something2-param2/somethin3-param3

Can i rewrite it with htaccess to

http://domain/phpfile.php?something1=param1&something2=para2&something3=param3

also params are dynamic they can be 1,2,3 or 4

2
  • 2
    why don't you just rewrite it to r=something1-param1/something2-param2/somethin3-param3 and then use php to change that into something1=param1 etc Commented Feb 7, 2011 at 10:21
  • is it a typo that your second link has "para2" instead of "param2", or is this deliberate? Commented Feb 7, 2011 at 10:29

3 Answers 3

2

Just use:

Options +FollowSymLinks RewriteEngine On RewriteBase / RewriteRule ^phpfile.php/something1-([^/]+)/something2-([^/]+)/somethin3-([^/]+)$ phpfile.php?something1=$1&something2=$2&something3=$3 [QSA] 

Then in code Use $_GET

Sign up to request clarification or add additional context in comments.

Comments

1

You can use this simple solution

RewriteEngine On RewriteRule ^phpfile.php(.*)/([^/-]+)-([^/]*)/?$ phpfile.php$1?$2=$3 [QSA] 

It will work with as many params as you like.

Example: phpfile.php/name-walter/age-30 becomes phpfile.php?name=walter&age=30

2 Comments

Nice, the rule is recursing itself and appending each set of parameters to the query string as it goes? (matching the last set first and working backwards?)
Yes, exactly. The beautiful thing is that RewriteRule matches only the filename part. Every time the rule is matched it removes one parameter from the filename and adds it to the querystring... this is repeated until there are no more parameters.
1

If the list of parameters is going to be variable, and you don't want to include separate rules for each case in your .htaccess file, you might as well just catch the whole querystring and parse it internally.

You can use something like this RewriteRule ^phpfile.php/(.*)$ phpfile.php?params=$1 (untested), and then in your php file just manually parse the query string like this:

<?php #$_GET['params'] = 'something1-param1/something2-param2/somethin3-param3'; preg_match_all('/(\w+)-(\w+)/', $_GET['params'], $matches, PREG_SET_ORDER); foreach ($matches as $match) { $_GET[$match[1]] = $match[2]; } unset($_GET['params']); var_dump($_GET); ?> 

This will set up your $_GET superglobal to contain the key => value pairs as if they had been passed as individual parameters.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.