Blaise
Blaise

Reputation: 340

Defining optional $_GET variables using .htaccess

I'm working on a project where I have to create a CMS for a client. I'm trying to clean up my urls using .htaccess and have a static set of $_GET variables that can be used for all kinds of different applications throughout the whole site. I've looked and looked and haven't found anything that works exactly how I need it to...

So for example, I would like to have 4 or 5 variables that I can use at any time.

var1 = $_GET['action'];
var2 = $_GET['id'];
var3 = $_GET['target'];

So if I have

domain.com/user/new

it would be interpreted to domain.com/user.php?action=add

or if I had

domain.com/user/edit/3

it would function like:

domain.com/user.php?action=edit&id=3

and would not have to have the third variable filled out.

I also have a folder called admin for the CMS. Here's my current code:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f

# Remove .php
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteRule ^(admin)/([^/]+)/([^/]+)/([^/]+)/?$ /$1/$2?action=$3&id=$4 [L,QSA,NC]

#remove index.php from website root

RewriteCond %{THE_REQUEST} ^GET.*index [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

I'm really not familiar with htaccess at all so this is kind of frankensteined from other projects and other solutions I've see around the web.

This works for variable 1 but whenever I try to add anymore I can either get 2 variables to work, or 3 variables to work, but never all three at the same time.

Upvotes: 0

Views: 288

Answers (1)

Jorn
Jorn

Reputation: 457

I'm also working on a CMS myself an currently use this:

RewriteEngine on

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]

This will putt the whole url after domain.com/ in the $_GET variable url

I have one index.php page which handles all url requests and calls my classes or functions accordingly;

function url(){ 
    if(isset($_GET['url'])){
                $routes = [];
                // get the URL from the base defined in the.htaccess file.
                // filter url
                # Example: www.yourdomain.com/example-page/hello/1/title/

                $url = filter_var(trim($_GET['url']),FILTER_SANITIZE_URL);
                // delete last / if it is there.
                $url = rtrim($url,'/');
                # Exampele change 1: $url = example-page/hello/1/title

                /*
                 * Remove the - (dash) in the url : EX. example-page/hello. Classnames can't have the - (dash) so class is written as examplePage.
                 * to call the function we need to remove the - (dash)
                */

                # Example change 2: $url = examplepage/hello/1/title
                $url = str_replace('-','', $url );
                // create array with all the url parts.
                # Example change 3: $url = ["examplepage","hello",1,"title"]
                $url = explode('/',$url);

                // add all array values to the var routes.
                foreach($url as $value){
                    $routes[] = $value;
                }
            }
        return $routes;
}

Now you have an array $routes with as many values as you want. If you remain the same url structure in your application then you can use those array values as your get parameters. Of course properly prepare them for a database query.

Now you can access them by routes[0] etc. If you want an assocciative array and the structure is always the same you could try this:

$routes = ['action' => '','id' => '','target' => '']
for($i = 0;$i < count($url);$i++){
    $routes[$key] = $url[$i];
}

Now you could access them like $routes['action'];

Upvotes: 1

Related Questions