1

How does one make a view contain without using the get variable in PHP

I am trying to display post/id like this

www.domain.com/view/1234

instead of

www.domain.com/view.php?id=1234
Belzelga
  • 143
  • 1
  • 5
  • 16
  • It would be logical to display it is as `/view/1234` – Ryan Sep 24 '13 at 15:30
  • "How does one make a view contain without using the get variable in PHP" does not parse in English. Can you reword this? – Nathaniel Ford Sep 24 '13 at 15:31
  • 2
    You can specify redirects in the .htaccess file to send all requests to a distributor which checks the requests to route it to the appropriate handler. – Karan Punamiya Sep 24 '13 at 15:32
  • Please clarify your question as to what your use case is here. If you want to link to `/view/1234` that is simple enough. If you have no control over the uri, that is a very different question than if you do. – Nathaniel Ford Sep 24 '13 at 15:37

4 Answers4

3

Assuming you have an Apache server with the mod_rewrite module enabled, you can create a new .htaccess file with the following content:

// First of all we want to set the FollowSymlinks option, 
// and turn the RewriteEngine on
Options +FollowSymlinks
RewriteEngine on

// This is the rule that changes the URL
RewriteRule ^view/([^/.]+)/?$ view.php?id=$1 [L]

This would do exactly like you wanted and redirects any /view/123 to view.php?id=123.

Joseph Callaars
  • 1,770
  • 1
  • 19
  • 28
1

If what you're looking for is the ability to parse the uri, you can use this:

$uri = $_SERVER["REQUEST_URI"]
$parts = explode("/",$uri)

The last element in your $parts array should be your view id.

Drupal(1) contains the following function to do exactly this:

function arg($index) {
  static $arguments, $q;

  if (empty($arguments) || $q != $_GET['q']) {
    $arguments = explode('/', $_GET['q']);
    $q = $_GET['q'];
  }

  if (isset($arguments[$index])) {
    return $arguments[$index];
  }
}

(1) I do not advocate using Drupal, I merely use it as an example.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
  • It really depends on how you want to organize your pages, whether php on page, or url rewriting is the answer. – Christopher Stevenson Sep 24 '13 at 15:37
  • The question is pretty unclear. If the OP is trying to simply convert one to the other across an entire site where they don't have control or it would be a major rewrite, I can see that mod_rewrite would be the way to go. If, on the other hand, this is a one-off rewrite is massive overkill and probably more complicated than needed. – Nathaniel Ford Sep 24 '13 at 15:40
  • That reminds me of this saying: "I have a problem, so a used a [regular expression](http://www.regular-expressions.info/). Now I have two problems." – Christopher Stevenson Sep 24 '13 at 15:42
  • That's a ridiculous simplification. And it's unclear if rewrite is even what the OP is asking for. – Nathaniel Ford Sep 24 '13 at 15:50
1

This is done via webserver url rewriting.

Here's some references for Apache (using mod_rewrite):

Christopher Stevenson
  • 2,843
  • 20
  • 25
0

You need to rewrite the URL with htaccess and intercept it somehow using PHP and loading the file from somewhere.

  1. Rewrite URL with mod_rewrite (i.e.: domain.com/23234234/0 to domain.com?id=23234234&nr=0)
  2. Make a call to the database requesting the post with id 23234234
  3. Show it to the user

Is it what are you looking for?

(based on a previous answer)

Community
  • 1
  • 1
Ivo Pereira
  • 3,410
  • 1
  • 19
  • 24
  • 1
    You are welcome. If this solved your question, please mark it as solved by clicking on the green Correct icon :) – Ivo Pereira Sep 24 '13 at 15:38