How to get php request uri without context?

$_SERVER['REQUEST_URI'] returns a URI with a path outline.

For example, if the base URL of a website http://localhost:8080/sitename/(i.e. the path to the context is site_name), and I use $_SERVER['REQUEST_URI']for http://localhost:8080/sitename/portfolio/design, it will return /sitename/portfolio/design.

Then I expand the result to interpret my clean urls:

$page=$_SERVER['REQUEST_URI'];
$segments=explode('/',trim($page,'/'));

$sitePage = $segments[1];//portfolio

This works fine for the local test environment, but on the production server $segments[1]should become$segments[0]

To use the same code in development and production, is there a way to get only this part /portfolio/design, that is, a URI without a context path?

+3
source share
3 answers

You can use virtual hosts to enable your test site:

http://sitename.localhost:8080/

, ( -) .

.

+9

, SCRIPT_NAME REQUEST_URI :

// /sitename/index.php
$path = explode('/', trim($_SERVER['SCRIPT_NAME'], '/'));

// /sitename/portfolio/design
$uri  = explode('/', trim($_SERVER['REQUEST_URI'], '/'));

foreach ($path as $key => $val) {
    if ($val == $uri[$key]) {
        unset($uri[$key]);
    } else {
        break;
    }
}

// portfolio/design
$uri = implode('/', $uri);

, .htaccess routing to index.php

, .

+4

I would say that this is the way to do what you would like to do - get a part of the URI.

I would suggest you set up a virtual host (assuming you are using Apache) based on the name or address. Then you can have something like http://127.0.0.42/portfolio/design or, with some editing of the host file on your system, even http: // sitename / portfolio / design . Then your development environment will be similar to the production one.

Hope this helps.

0
source

All Articles