简体   繁体   中英

rewrite URL and get parameter

at the moment I have this url format:

www.domain.de/?paramter=value

with php I can access the value like this:

<? echo $_GET['paramter']; ?>

but I would like to rewrite the url to this format:

www.domain.de/paramter=value

How can I realize this with a htaccess line and how can I access it with php after rewrite ?

please try this:

$g = $_GET;

$new_str = '';
foreach($g as $key => $value) {
    $new_str .= "$key=$value";
} 

echo $_SERVER['SERVER_NAME'] ."/". $new_str;

Using the following URL:

http://site.test/i.php?p1=1&p2=2&p3=3 ,

It will return:

site.test/p1=1p2=2p3=3

Basically this code loops through the $_GET parameters and then grabs the key and the value, turns them into a string and then write a final URL with the string. You can format the output as you wish.

www.domain.de/paramter=value

is a pretty strange way of putting values. However, whenever a user enters such URL, you can make sure that the user lands on the correct page via below .htaccess on Apache server

RewriteEngine On
RewriteRule ^/?([^=]+)=(.+)$ /?$1=$2 [L,NC,R=302]

Demo: https://htaccess.madewithlove.be?share=53dc007b-3781-4d1d-80c6-5f0234cdf11a

This way, your PHP code is intact and nothing needs to be changed on the backend code. If you wish to make a permanent redirection, make R = 301 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM