重写GET变量php


Rewrite GET variables php

我想重写,我的脚本是这样的:

<?php
$id = $_GET['id'];
if($id) {
echo "Your id are $id";
} else {
echo "Id are missing";
}
?>

有了.htaccess,我想在我的url中这样做:http://example.com/something/id

那么,我必须在php中添加什么edit并添加到.htaccess中?

在Root/.htaccess 中尝试以下操作

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /file.php?id=$1 [NC,L]

将/file.php替换为您的文件。

这将在内部重定向

example.com/123

example.com/file.php?id=123

您可以通过这种方法自动获取变量值

RewriteEngine On
RewriteBase / 
RewriteEngine On Options All -Indexes RewriteBase /directoryname/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    ###############  SEO     ##########################
#
http://www.example.com/hello/booboo/ it takes the url  after .com/
    RewriteRule ^(.*)$ getme.php?url=$1 [QSA,L]

例如,如果我输入http://www.example.com/hello/bobo/

它将替换并接受"/hello/bobo/"部分,现在你可以在.htacces文件中使用它。如果你想重定向到另一个页面并过滤变量值,你应该修改$1,因为其中的所有数据。

edit:在这个例子中,我在我的域后获得url,然后重定向到get.php。你也可以使用split方法将url除以"/"。让我们看看get.php页面来理解这个方法

getme.php:

<?php
//we redirect to get in url=$1 so our get method name is url
$parca = explode("/", $_GET["url"]); //and we divided by slash the url.
echo $parca[0];//this is first part "/hello/
echo $parca[1];// and this is second part "/booboo/;
?>