0

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

2 Answers 2

1
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'; ?>"/>
Sign up to request clarification or add additional context in comments.

14 Comments

Hey, thanks for the answer, but for some reason this doesn't work. If I add your code, it doesn't even include the file anymore
I just modified..forgot to add something...try that and let me know if it works or not
Still doesn't work. All I want is to replace index.php?p=account&settings with /account/settings
look at edit. I even set this entire scenario up on one of my sites. It works.
note that the $_GET['settings'] the way you are setting it up is not returning ANY results. All it does it sets the variable settings. You don't have &settings=xxxxxxxx you only have &settings
|
0

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]

1 Comment

Could you explain what is meant by ^post/ ? I'm new to these rewrite things

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.