1

StackOverflow has a very neat, clean URL format. It looks the same as a directory structure, but there can't be a directory for each question on here! My question is this:

How can I get http://www.site.com/sections/tutorials/tutorial1, for example, to stay like that in the address bar, but convert it to a $_GET request for PHP to mess around with?

I could use a .htaccess file, but I don't want the URL being rewritten - I'd like it to remain clean and friendly. Is my only option here to use PHP's string splitting functions to get some pretend $_GET data?

Thanks,

James

Bojangles
  • 99,427
  • 50
  • 170
  • 208
  • Unless you make it redirect, `.htaccess` 's `ModRewrite` only rewrites the URLs server-side. Client-side, the user keeps the clean URL. – Eric Dec 17 '10 at 12:06

5 Answers5

1

What about this, using .htaccess to split the URL up, the URL won't change but instead point to index.php with various $_GET variables, this could could be increased to cover more URL sections.

# turn rewriting on
RewriteEngine on

# get variables in this order, [object], [object,action], [object,action,selection]
RewriteRule ^([^/\.]+)/?$ /index.php?object=$1 [L,NC,QSA]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?object=$1&action=$2 [L,NC,QSA]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?object=$1&action=$2&selection=$3 [L,NC,QSA]
Ben Everard
  • 13,652
  • 14
  • 67
  • 96
0

A PHP Rest framework could do this for you, so I refer you to this question. Most of the frameworks won't load the data from $_GET, but will offer a similar and equally convenient way to read it.

Community
  • 1
  • 1
kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • Thanks for your recommendation, but I'd rather not use a framework - I only want the one feature outlined in my OP, so a framework would be overkill. When it comes to developing the rest of my site (quite a while), I may consider one :-) – Bojangles Dec 15 '10 at 16:44
0

If you don't want to use mod rewrite the best solution would be using regular expressions agains the $_SERVER['REQUEST_URI'] variable.

i.e:

preg_match('|/(.*)/(.*)|', $_SERVER['REQUEST_URI'], $match);
$_GET['param1'] = $match[1];
$_GET['param2'] = $match[2];

If you want to setup a capture all php script. IE if the script request doesn't exist use a default script, use mod-rewrite to redirect everything to one script i.e. the zend framework (and most of the PHP MVC framework) use this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

I think that could be a bit cumbersome.

RageZ
  • 26,800
  • 12
  • 67
  • 76
  • Thank you for the mod_rewrite idea - I completely forgot about that. Do I need to edit apache config files, however? I can't get access to my file system that easily... – Bojangles Dec 15 '10 at 09:30
  • @JamWaffles: if you want to do a capture all php script just use mod rewrite – RageZ Dec 15 '10 at 09:32
0

It's actually a RESTful way of building your URI's. Not only SO applies this pattern. I recommend to not re-invent the wheel by taking a look at this question.

In addition you could switch over to a RESTful framework such as CakePHP or CodeIgniter, which are configured by default to use the RESTful pattern.

Community
  • 1
  • 1
thomaux
  • 19,133
  • 10
  • 76
  • 103
0

$_GET does not contain the path compontents from the URL, only the parameters that eventually follow the ?. You could use

$parts = explode('/', pathinfo($_SERVER['REQUEST_URI'], PATHINFO_DIRNAME));
var_dump($parts);

However it seems you should have a read on URL rewriting e.g. with mod_rewrite. "I don't want the URL being rewritten - I'd like it to remain clean and friendly" ... The rewriting happens on the server. The user never sees the "ugly" result.

rik
  • 8,592
  • 1
  • 26
  • 21