简体   繁体   中英

PHP Rewrite multiple parameters at once with /param1/param2

I'd like to know how to rewrite multiple paramaters into /param1/param2

Let's assume I have included a file called account.php with

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

It works just fine and is rewritten to

http://myurl.com/account

but let's say I want to have multiple parameters in account.php like

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

which should rewritten look like:

http://myurl.com/account/settings How would I do that ?

.htacces file:

<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 Code to include the files:

 $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';
 }
RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+)/?$ index.php\?p=$1&action=$2
RewriteRule ^([a-zA-Z]+)/?$ index.php\?p=$1

for each ([a-zA-Z]+) it adds another number to the "results".. $1, $2, $3, etc. So you can add as many rewrites as you want... for example, this is one of my mod rewrites...

#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

on the php page, just add $_GET['action'];

and then set up your php to where if an action is set for the page, go to that action (in this case, settings).

I just used "action" as an example...it can be whatever you want it to be.

EDIT

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

my php code to test results

$p = 'home';

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

echo ' ';

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

I went to domain.com/profile/settings and it echoed the following: p:profile yes

fixing includes

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'); }

This will help ignore the "fake folders" in the url

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

this is a php function that will identify whether your website is using http:// or https:// for the absolute paths


this is the function to get your website url for absolute paths

// ---------------------------------------- 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];
}

example of how to add the code to your site

<link rel="stylesheet" type="text/css" href="<?php echo http() . $websitedomain .'/WMS/css/style.css'; ?>"/>

I work with this code in my website is does what you looking for

RewriteEngine on 
RewriteBase /

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

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