hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : PHP variables as directories
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

PHP variables as directories

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 05-28-2007, 11:34 AM
Powi Powi is offline
WHT Addict
 
Join Date: Apr 2004
Posts: 167

PHP variables as directories


Here's an example of what i'm trying to do...

http://good-tutorials.com/search/tutorials/car

On this url, "car" is a variable, but it is passed as if it was a dir, and not like /tutorials.php?search=car

Can anyone help me do this?

Hope you understand

Powi

__________________
cuartopunto.com

Reply With Quote


Sponsored Links
  #2  
Old 05-28-2007, 11:46 AM
spt1224 spt1224 is online now
Junior Guru Wannabe
 
Join Date: Feb 2007
Location: Louisville, Kentucky
Posts: 31
I have never done this, but I know that MediaWiki, among others, does this. I'm pretty sure that they use Apache URL rewrite directives (or the equivalent for other servers) in .htaccess files.

I can elaborate, if you wish.

Reply With Quote
  #3  
Old 05-28-2007, 12:27 PM
mwatkins mwatkins is offline
Web Hosting Master
 
Join Date: Nov 2001
Location: Vancouver
Posts: 2,416
PHP natively doesn't allow for this.

First attempts in PHP to create "nice" urls where the path expressed a series of objects and methods or "variables" as you say were done by using mod_rewrite.

I have done this; to me url design (this predates REST as an architectural style but was alone the same lines of thinking) is important. It encourages good clean app design and helps ensure your URL's will have a fair chance of being permanent.

But using mod_rewrite and equiv. is horribly clunky and cumbersome, especially if you have a url schema that is more than just a couple of objects and methods.

That's one of the reasons I left PHP years ago, in favour of Python.

At the time Zope (and its ancestors) had a very cool method of exposing an object via the web that gave you sensible URL's by default, for example:

http://foo.com/cars/fairlane/
http://foo.com/cars/fairlane/edit
http://foo.com/cars/fairlane/rss
http://foo.com/cars/rss

or

http://knowit.com/tutorials/car

No need to embed the term "search" in there when what you want is the "car" object.

Anyway, I looked into Zope. It had a lot of power but was overly complex. At the same time another Python web framework that essentially replicated the same technique for "publishing objects via the web" was born called Quixote. I've been using Quixote or its cousin QP ever since.

Now PHP has "web frameworks" too, but at the time there was essentially nothing of similar power, and the language itself lacked the type of expressiveness that Python had. I went from being a relatively experienced PHP (and ASP of the day) developer to a Python newbie, totally wet behind the ears, but was doing project work at double the effectiveness or more within just a few weeks.

Quixote, QP (and a terrific object database called Durus):
http://www.mems-exchange.org/software/

Zope, the grandaddy of them all:
http://www.zope.org/

The most visible Python web frameworks out there these days all do what QP and Quixote and Zope have been doing for years - and are also worth serious consideration:

Django: http://www.djangoproject.com/
TurboGears: http://www.turbogears.org/
Pylons: http://pylonshq.com/

Ruby, another object oriented language, also has an extremely popular web framework called Rails:
http://rubyonrails.org/

Quite often a developer given Python or Ruby code to look at will intuitively prefer one or the other. They have different styles. Python is probably easier to learn as it reads more like english; experienced developers coming from other languages such as Java would find either fits their brain but one will appear more. Pick either, both are good. I happen to prefer the look and readability of Python.

Hopefully someone will point out some PHP frameworks that do something similar to "object publishing".

__________________
“Even those who arrange and design shrubberies are under
considerable economic stress at this period in history.”


Last edited by mwatkins; 05-28-2007 at 12:34 PM.
Reply With Quote
Sponsored Links
  #4  
Old 05-28-2007, 02:52 PM
kuku kuku is offline
Newbie
 
Join Date: Mar 2007
Posts: 16
make an .htaccess file:

RewriteEngine on
RewriteBase /

RewriteRule ^([A-z]{1,25})/([A-z0-9]{1,27})[/]{0,1}index.php?module=$1&action=$2&%1 [L]
RewriteRule ^([A-z]{1,25})[/]{0,1}$ index.php?module=$1&action=none [L]


www.domain.com/client/add/

will get rewritten as: http://www.domain.com/index.php?modu...ent&action=add

__________________
Managed Services
Chicago Custom Dedicated Hosting
Aim: Rafal9

Reply With Quote
  #5  
Old 05-28-2007, 03:22 PM
mwatkins mwatkins is offline
Web Hosting Master
 
Join Date: Nov 2001
Location: Vancouver
Posts: 2,416
Oh come on now, where is the fun in simply answering the question?

(just kidding kuku)

__________________
“Even those who arrange and design shrubberies are under
considerable economic stress at this period in history.”

Reply With Quote
  #6  
Old 05-29-2007, 12:25 PM
Powi Powi is offline
WHT Addict
 
Join Date: Apr 2004
Posts: 167
WORKED! thanks a lot to all of you!

Just one more question:

What does the "{1,27}" part of the RewriteRule means??

Thanks again

Powi

__________________
cuartopunto.com

Reply With Quote
  #7  
Old 05-29-2007, 01:11 PM
kuku kuku is offline
Newbie
 
Join Date: Mar 2007
Posts: 16
It means it will match the rule if there are between 1 and 27 characters.

__________________
Managed Services
Chicago Custom Dedicated Hosting
Aim: Rafal9

Reply With Quote
  #8  
Old 05-29-2007, 01:53 PM
sasha sasha is offline
Hail Eris !
 
Join Date: Oct 2002
Location: Canada
Posts: 3,100
Here is my default .htaccess

There is one limitation though, you can capture only 4 value/key pairs this way. If someone knows a way around it, I would love to know it.

Code:
Options -Indexes
DirectoryIndex index.php
# error pages
ErrorDocument 400 /index.php?page=front_error&code=400
ErrorDocument 401 /index.php?page=front_error&code=401
ErrorDocument 403 /index.php?page=front_error&code=403
ErrorDocument 404 /index.php?page=front_error&code=404
ErrorDocument 500 /index.php?page=front_error&code=500

RewriteEngine on
# keep the page rank on www
RewriteCond %{HTTP_HOST} ^example.com\.com
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]

# standard link rewriting
RewriteRule ^content/([a-zA-Z0-9]+)/([^/]+)/([a-zA-Z0-9]+)/([^/]+)/([a-zA-Z0-9]+)/([^/]+)/([a-zA-Z0-9]+)/([^/]+) index.php?$1=$2&$3=$
RewriteRule ^content/([a-zA-Z0-9]+)/([^/]+)/([a-zA-Z0-9]+)/([^/]+)/([a-zA-Z0-9]+)/([^/]+) index.php?$1=$2&$3=$4&$5=$6 [L]
RewriteRule ^content/([a-zA-Z0-9]+)/([^/]+)/([a-zA-Z0-9]+)/([^/]+) index.php?$1=$2&$3=$4 [L]
RewriteRule ^content/([a-zA-Z0-9]+)/([^/]+) index.php?$1=$2 [L]
RewriteRule ^content index.php [L]
Sample url:

http://example.com/content/page/test.../blahblah.html
will be rewritten as:

http://www.example.com/index.php?pag...ubsection=love

Reply With Quote
  #9  
Old 05-29-2007, 02:01 PM
Anatoly_ua Anatoly_ua is offline
Newbie
 
Join Date: Dec 2004
Location: Kherson, Ukraine
Posts: 13
Still another option could have been (at least this workd best for me): put in .htaccess :
Code:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)  index.php
and then in index.php process the variable $_SERVER['REQUEST_URI']...

Reply With Quote
  #10  
Old 05-30-2007, 12:44 PM
Powi Powi is offline
WHT Addict
 
Join Date: Apr 2004
Posts: 167
Done it! Worked! thanks!

__________________
cuartopunto.com

Reply With Quote
  #11  
Old 05-30-2007, 12:53 PM
superdooper superdooper is offline
WHT Addict
 
Join Date: Aug 2006
Location: Upavon, England
Posts: 144
out of interest, which option did you use?

__________________
James Bone, citruszebra
citruszebra.cz // 03333 404 100 // cz-at-citruszebra.cz

Any views are my own and not of any company I may represent.

Reply With Quote
  #12  
Old 05-30-2007, 01:10 PM
mwatkins mwatkins is offline
Web Hosting Master
 
Join Date: Nov 2001
Location: Vancouver
Posts: 2,416
Anatoly's "option" provides a lot more flexibility but requires that your PHP code parse out the request URI; the other ReWrite solution first proposed does a direct 1:1 mapping between URI patterns and pages/functions.

The former could be used to build a dispatch system that would look more like the Python oriented solutions I discussed, but would take some work. No doubt some PHP web frameworks take such a road.

The pattern based ReWrite rule solution first proposed on the thread is fairly easy to bolt on to an existing, working site, delivering the principal benefit of a cleaner URL to the user.

I'm betting the OP chose that root (not Anatoly's option).

__________________
“Even those who arrange and design shrubberies are under
considerable economic stress at this period in history.”

Reply With Quote
  #13  
Old 06-19-2007, 03:15 PM
Powi Powi is offline
WHT Addict
 
Join Date: Apr 2004
Posts: 167
I finally chose kuku's option but changed it a little bit. It's working great.

Now I have one more question:

PHP Code:
RewriteRule ^([A-z0-9]+)[/]{0,1}$ index.php?module=$[L
Will rewrite all directories...

What if i want it to do this for all dirs, EXCEPT for the /img dir?

Thanks again!

Powi

__________________
cuartopunto.com

Reply With Quote
  #14  
Old 06-19-2007, 03:31 PM
kuku kuku is offline
Newbie
 
Join Date: Mar 2007
Posts: 16
tada

RewriteCond %{REQUEST_URI} !^/img

__________________
Managed Services
Chicago Custom Dedicated Hosting
Aim: Rafal9

Reply With Quote
  #15  
Old 07-11-2007, 04:23 PM
ApplePro ApplePro is offline
WHT Addict
 
Join Date: Oct 2005
Posts: 101
Quote:
Originally Posted by kuku View Post
make an .htaccess file:

RewriteEngine on
RewriteBase /

RewriteRule ^([A-z]{1,25})/([A-z0-9]{1,27})[/]{0,1}index.php?module=$1&action=$2&%1 [L]
RewriteRule ^([A-z]{1,25})[/]{0,1}$ index.php?module=$1&action=none [L]


www.domain.com/client/add/

will get rewritten as: http://www.domain.com/index.php?modu...ent&action=add
I've just tried to put this .htaccess to directory "a" on my server. But when I run this URL in my browser:

http://www.example.com/a/france/paris/

I get 404 error. Am I doing something wrong?

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
Cloud Converter Launches Free Service for Migrating Cloud Servers Web Hosting News 2013-03-19 11:23:45
Cloud Infrastructure Firm Intelicloud Acquires Web Host Slice Networks Web Hosting News 2012-05-02 09:50:03
TDS Completes Data Center Relocation for University of Texas Web Hosting News 2011-08-26 19:49:32
Top 50 Cloud Computing Services Highlighted in New Report Web Hosting News 2011-06-15 20:52:10
Sony Estimates $171M in Losses from PlayStation Network Outage, More from Earthquake Web Hosting News 2011-05-24 14:07:38


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?