PHP使用/ param1 / param2一次重写多个参数

问题描述 投票:0回答:2

我想知道如何将多个参数重写到/ param1 / param2中

我们假设我已经包含了一个名为account.php的文件

http://myurl.com/index.php?p=account

它工作正常,并被重写

http://myurl.com/account

但是我要说我想在account.php中有多个参数

http://myurl.com/index.php?p=account&settings

应重写的内容如下:

http://myurl.com/account/settings我该怎么做?

.htaccess文件:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>

PHP代码包含文件:

 $pages = array('products','about','impressum','home','support','solutions','mc','team','account');

 $p = 'home';
 if(isset($_GET['p'])) {
 $p = $_GET['p'];
 }
 if(in_array($p, $pages)){
 include 'includes/'.$p.'.php';
 }
 else {
  include 'includes/home.php';
 }
php apache .htaccess rewriting
2个回答
0
投票

我在我的网站上使用这个代码就是你要找的东西

RewriteEngine on 
RewriteBase /

RewriteRule ^post/([^/]+)/([^/]+)$ single.php?post_type=$1&post_id=$2 [QSA]

1
投票
RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+)/?$ index.php\?p=$1&action=$2
RewriteRule ^([a-zA-Z]+)/?$ index.php\?p=$1

对于每个([a-zA-Z]+),它会在“结果”中添加另一个数字.. $ 1,$ 2,$ 3等。所以你可以添加任意数量的重写...例如,这是我的mod重写之一...

#WMS LIVE EDITOR
RewriteRule ^WMS/templateeditor/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ WMS/templateeditor.php\?p1=$1&p2=$2&p3=$3&p4=$4
RewriteRule ^WMS/templateeditor/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ WMS/templateeditor.php\?p1=$1&p2=$2&p3=$3
RewriteRule ^WMS/templateeditor/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ WMS/templateeditor.php\?p1=$1&p2=$2
RewriteRule ^WMS/templateeditor/([a-zA-Z0-9]+)/?$ WMS/templateeditor.php\?p1=$1
RewriteRule ^WMS/templateeditor/?$ WMS/templateeditor.php

在php页面上,只需添加$_GET['action'];

然后将php设置为如果为页面设置了操作,请转到该操作(在本例中为设置)。

我只是用“行动”作为例子......它可以是你想要的任何东西。

编辑

RewriteEngine On
RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+)/?$ index.php\?p=$1&$2
RewriteRule ^([a-zA-Z]+)/?$ index.php\?p=$1

我的PHP代码来测试结果

$p = 'home';

if(isset($_GET['p'])) {
 $p = $_GET['p'];
     echo 'p:'. $p;
 }

echo ' ';

if(isset($_GET['settings'])) {
 echo 'yes';
 }

我去了domain.com/profile/settings,它回应了以下内容:p:profile yes

修复包括

if(file_exists('rootDir.php')) { include_once ('rootDir.php'); }
if(file_exists('../rootDir.php')) { include_once ('../rootDir.php'); }
if(file_exists('../../rootDir.php')) { include_once ('../../rootDir.php'); }
if(file_exists('../../../rootDir.php')) { include_once ('../../../rootDir.php'); }
if(file_exists('../../../../rootDir.php')) { include_once ('../../../../rootDir.php'); }
if(file_exists('../../../../../rootDir.php')) { include_once ('../../../../../rootDir.php'); }
if(file_exists('../../../../../../rootDir.php')) { include_once ('../../../../../../rootDir.php'); }

这将有助于忽略网址中的“假文件夹”

// ---------------------------------------- http() ---- determine if http/https
if (!function_exists('http')) {
    function http() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == 'on') {$pageURL .= 's';}
 $pageURL .= '://';
 return $pageURL;
}
}

这是一个php函数,可以识别您的网站是使用http://还是https://作为绝对路径


这是获取绝对路径的网站网址的功能

// ---------------------------------------- parse URL ---- split URL into parts
$websiteurl = http() . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI];
$website = parse_url($websiteurl);
$websitedomain = $website['host'];
if (strpos($websiteurl, '~') !== FALSE) {
    $subweb = explode('/', $websitedomain . $_SERVER[REQUEST_URI]);
    $websitedomain = $subweb[0] .'/'. $subweb[1];
}

如何将代码添加到您的站点的示例

<link rel="stylesheet" type="text/css" href="<?php echo http() . $websitedomain .'/WMS/css/style.css'; ?>"/>
© www.soinside.com 2019 - 2024. All rights reserved.