Php 将网页更改为';目录';
我不知道你怎么称呼它,但谁能帮我解决这个问题: 有没有办法将.php文件设置为目录 例如: 我在同一个目录中有两个文件:onepage.php和secondpage.php。如果我想访问index.php,我会去..../onepage.php(比如:www.stackoverflow.com/onepage.php)。然后,如果我想访问第二个页面,我会访问www.stackoverflow.com/secondpage.php。但是,是否有办法使其在www.stackoverflow.com/secondpage上可见 另外,如果我举同一个例子,假设我在secondpage.php上有一个$GET,是否还有一种方法可以实现www.stackoverflow.com/secondpage/($u GET value)Php 将网页更改为';目录';,php,html,directory,Php,Html,Directory,我不知道你怎么称呼它,但谁能帮我解决这个问题: 有没有办法将.php文件设置为目录 例如: 我在同一个目录中有两个文件:onepage.php和secondpage.php。如果我想访问index.php,我会去..../onepage.php(比如:www.stackoverflow.com/onepage.php)。然后,如果我想访问第二个页面,我会访问www.stackoverflow.com/secondpage.php。但是,是否有办法使其在www.stackoverflow.com/
谢谢 这可以通过使用Apache服务器的
mod\u rewrite
和.htaccess
来实现。设置规则以有效地重写可见的URL。使用.htaccess
文件中的mod\u rewrite
。如下代码:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule secondpage/(.*)$ index.php?var1=$1 [L]
RewriteEngine On
# Run everything else but real files through parse.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|png|html|js|json|jpg|jpeg)$
RewriteRule ^(.*)$ parse.php?info=$1 [L]
<?php
$getVars = $_GET['info'];
$vars = explode("/",$getVars);
if(count($vars)<1){
$vars[0] = "home";
$vars[1] = "index";
}else if(count($vars)<2){
$vars[1] = "index";
}
$fileString = $vars[0] . '/' . $vars[1] . '.php';
if(file_exists($fileString)){
include_once($fileString);
}else{
include_once("error/404.php");
}
现在,$\u GET['var1']
是您的变量。
祝你好运。这是从我的网站复制的工作代码 创建一个名为.htaccess的文件 添加此代码:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule secondpage/(.*)$ index.php?var1=$1 [L]
RewriteEngine On
# Run everything else but real files through parse.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|png|html|js|json|jpg|jpeg)$
RewriteRule ^(.*)$ parse.php?info=$1 [L]
<?php
$getVars = $_GET['info'];
$vars = explode("/",$getVars);
if(count($vars)<1){
$vars[0] = "home";
$vars[1] = "index";
}else if(count($vars)<2){
$vars[1] = "index";
}
$fileString = $vars[0] . '/' . $vars[1] . '.php';
if(file_exists($fileString)){
include_once($fileString);
}else{
include_once("error/404.php");
}
现在,在parse.php文件中有以下代码:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule secondpage/(.*)$ index.php?var1=$1 [L]
RewriteEngine On
# Run everything else but real files through parse.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|png|html|js|json|jpg|jpeg)$
RewriteRule ^(.*)$ parse.php?info=$1 [L]
<?php
$getVars = $_GET['info'];
$vars = explode("/",$getVars);
if(count($vars)<1){
$vars[0] = "home";
$vars[1] = "index";
}else if(count($vars)<2){
$vars[1] = "index";
}
$fileString = $vars[0] . '/' . $vars[1] . '.php';
if(file_exists($fileString)){
include_once($fileString);
}else{
include_once("error/404.php");
}
quick win将在根目录中添加一个名为secondpage的文件夹,然后添加index.php文件,添加代码以捕获get请求