views:

64

answers:

1

I have written the following code in my .htaccess file

Options +FollowSymLinks
RewriteEngine on
RewriteRule page/(.*)/ index.php?page=$1&%{QUERY_STRING}
RewriteRule page/(.*) index.php?page=$1&%{QUERY_STRING}

The url "xyz.in/index.php?page=home" will look like this in the address bar of browser "xyz.in/page/home"

If I want to pass a variable through URL than I will have to write as "xyz.in/page/home?value=1" or "xyz.in/page/home?value=1&value2=56&flag=true"

The initial part of url (xyz.in/page/home) is clean(search engine friendly), but if I pass some more variables in the url then it doesn't look nice.

I want to make this url like "xyz.in/page/home/value/4/value2/56" and so on.

The variables value and value2 are not static they are just used for example over here. Name can be anything.

Is it possible to do this ?

Please help me form the ".htaccess" file

(any corrections related to title or language or tags used in this question are welcome)

Thanks

+1  A: 

The easiest would be to parse the URL path with PHP. Then you would just need this rule to rewrite the requests to your PHP file:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^index\.php$ index.php [L]

The condition will ensure that only requests to non-existing files are rewritten.

Your PHP script could than look like this:

$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$segments = explode('/', ltrim($_SERVER['REQUEST_URI_PATH']));
for ($i=0, $n=count($segments); $i<$n; $i+=2) {
    $_GET[rawurldecode($segments[$i])] = rawurldecode($segments[$i+1]);
}
Gumbo
Thanks for helping Mr. Gumbo,But How is it usable then.? Could you elaborate your answer please?
Gaurav Sharma
@Gaurav Sharma: You can use the code just like I wrote it. A request for `/page/home/value/4/value2/56` should then be parsed to `$_GET = array('page'=>'home', 'value'=>'4', 'value2'=>'56');`.
Gumbo
You can also use parse_url($_SERVER['REQUEST_URI'])['path'] to get path without using regex.
Imran
@Imran: It’s rather `parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)`.
Gumbo
:-( not working. ok let me make it more clear., I wrote the code as suggested by you in my home.php file and created a new .htaccess file accordingly.But when I type the url 'xyz.in/page/home/value4/value2/56' the browser says object not found.
Gaurav Sharma
@Gaurav Sharma: Well, it works for me. I hope you didn’t forget the mandatory `RewriteEngine on`.
Gumbo