vkris
vkris

Reputation: 2165

URL Manipulation PHP

I have a url something like

http://something.com/abc/def/file.php/arguments

This simply executes final.php and /arguments is passed to $_SERVER['PATH_INFO'] variable.

I want to execute the same but without the '.php' i.e,

http://something.com/abc/def/file/arguments

I am guessing I need to add something to http.conf, or...?

Upvotes: 0

Views: 672

Answers (4)

vitto
vitto

Reputation: 19476

This URL style can be managed by the url_rewrite (called url rewriting) and it can be done with .htaccess file of your Apache server.

to do it you'll need to write this in you .htaccess file:

RewriteEngine On
RewriteRule ^http://something.com/every/name/you/like/(arguments)/?$    server_folder/page.php?argument_var=$1

The first block of code, rapresents the user called page:

^http://something.com/every/name/you/like/(arguments)/?$

The second block is the real page you want to call, where $1 is the var value inside the ()

server_folder/page.php?argument_var=$1

If the user must go to an URL where the arguments are numbers only you should insert:

^http://something.com/every/name/you/like/([0-9])/?$

If the user must go to an URL where the arguments are letters only you should insert:

^http://something.com/every/name/you/like/([a-zA-Z])/?$

To work correctly with this URL style you'll need to understand a little bit of regular expresions like in this link.

You could find useful this table to help you understand something more.

Note you can write different URL instead of the real page name like:

^http://something.com/love/([a-zA-Z0-9])/?$    section/love/search.php?$1

This should be useful to hide server pages.

Upvotes: 0

Jason Clawson
Jason Clawson

Reputation: 1027

.htaccess is your friend

Options +FollowSymLinks
RewriteEngine on
RewriteRule file/(.*) file.php?param=$1

Upvotes: 3

Vlad.P
Vlad.P

Reputation: 1464

So you want to rewrite your URL's. Have a look here: http://corz.org/serv/tricks/htaccess2.php

Upvotes: 0

RobertPitt
RobertPitt

Reputation: 57268

I think the best way to do this is to adopt the MVC style url manipulation with the URI and not the params.

In your htaccess use like:

<IfModule mod_rewrite.c>
    RewriteEngine On
    #Rewrite the URI if there is no file or folder
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

Then in your PHP Script you want to develop a small class to read the URI and split it into segments such as

class URI
{
   var $uri;
   var $segments = array();

   function __construct()
   {
      $this->uri = $_SERVER['REQUEST_URI'];
      $this->segments = explode('/',$this->uri);
   }

   function getSegment($id,$default = false)
   {
      $id = (int)($id - 1); //if you type 1 then it needs to be 0 as arrays are zerobased
      return isset($this->segments[$id]) ? $this->segments[$id] : $default;
   }
}

Use like

http://mysite.com/posts/22/robert-pitt-shows-mvc-style-uri-access

$Uri = new URI();

echo $Uri->getSegment(1); //Would return 'posts'
echo $Uri->getSegment(2); //Would return '22';
echo $Uri->getSegment(3); //Would return 'robert-pitt-shows-mvc-style-uri-access'
echo $Uri->getSegment(4); //Would return a boolean of false
echo $Uri->getSegment(5,'fallback if not set'); //Would return 'fallback if not set'

Now in MVC There usually like http://site.com/controller/method/param but in a non MVC Style application you can do http://site.com/action/sub-action/param

Hope this helps you move forward with your application.

Upvotes: 2

Related Questions