重写GET变量php

时间:2015-12-12 13:50:47

标签: php .htaccess mod-rewrite

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

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

.htaccess我想在我的网址中这样做:http://example.com/something/id

那么,我edit中的php必须添加到我的.htaccess

2 个答案:

答案 0 :(得分:2)

在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

答案 1 :(得分:1)

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

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/

它将取代并采取&#34; / hello / bobo /&#34;现在你可以在.htacces文件中使用它。另外如果你想重定向到另一个页面并过滤变量值,你应该修改$ 1,因为这里有所有数据。

编辑:在该示例中,我获取了域名之后的网址,并且我重定向到了get.php,您也可以使用拆分方法将网址划分为&#34; /&#34;让我们看一下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/;
?>
相关问题