Creating PHP GET Parameters Looks Like Directories

I am trying to do this:

http://foo.foo/?parameter=value "converts" to http://foo.foo/value

Thank.

+5
source share
4 answers

Assuming you are running on Apache, this code in .htaccess works for me:

RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)/$ /index.php?parameter=$1

Depending on the structure of your site, you may need several rules.

+3
source

Enable mod_rewriteon the Apache server and use the rules .htaccessto redirect requests to the controller file.

.htaccess

# Enable rewrites
RewriteEngine On
# The following two lines skip over other HTML/PHP files or resources like CSS, Javascript and image files 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# test.php is our controller file
RewriteRule ^.*$ test.php [L]

test.php

$args = explode('/', $_SERVER['REDIRECT_URL']);  // REDIRECT_URL is provided by Apache when a URL has been rewritten
array_shift($args);
$data = array();
for ($i = 0; $i < count($args); $i++) {
    $k = $args[$i];
    $v = ++$i < count($args) ? $args[$i] : null;
    $data[$k]= $v;
}
print_r($data);

Accessing the URL http: // localhost / abc / 123 / def / 456 will result in the following:

Array
(
    [abc] => 123
    [def] => 456
)
+1
source

, Apache, :

. mod_rewrite.

0
source

Use mod rewrite rules if you are using Apache. This is the best and safest way to create a virtual directory.

0
source

All Articles