FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Managing URL structure in PHP





metalfreek
Is there a better way of managing URL structure in PHP without using framework? Codeigniter for example have a very nice and SEO friendly URL structure. Is there a simple way to achieve this effect.

I am looking for a URL structure like example.com/vehicles/two-wheeler/bike/
Marcuzzo
Codeigniter has got their library backing this up, they also explode the url and then the first part after the domain name will be the controller ( which is a subclass of CI_Controller) , the second one ( if any ) will be the public method within that controller. and the rest of the segments are parameters to the called method


this approach is the only way I guess.
When you explode the url you need to check the length of the array so you know what you are looking at and you just parse away.
jmraker
It's possible to do that without a framework. First you need to edit your .htaccess to add something like

Code:

RewriteEngine On

RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ error.php [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$  [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]


That should redirect every page request to a page that doesn't exist to your /index.php

Inside your /index.php you'd get the URL that was originally requested with
Code:
$origURL = $_SERVER['REDIRECT_URL'];

so for http://example.com/vehicles/two-wheeler/bike/
$origURL would be '/vehicles/two-wheeler/bike/' which you can explode on the letter '/' or use a regular expression to match it.

FYI: If the url was http://example.com/vehicles/two-wheeler/bike/?a=1&b=2
The ?a=1&b=2 isn't in $_SERVER['REDIRECT_URL'], it's removed and put in $_GET and $_REQUEST as usual.

/index.php will be called on just about every url so it would have to handle invalid url requests
codersfriend
jmraker wrote:
It's possible to do that without a framework. First you need to edit your .htaccess to add something like

Code:

RewriteEngine On

RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ error.php [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$  [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]

...
Yeah I agree with the htaccess code. You will just be needing to do a lot of matching to your parameters. Mostly you would need to understand regexp
Arrogant
This was of great help to me aswell. my friend was talking about this and now i understand how to implement this
Related topics
script backup database
site colse/open with php
Getting a URL with PHP
PHP - Error
Select a url for an image in image functions
PHP script problem
php/sql question
Flash over php
Getting PHP variable to work in FCKeditor
rss 2.0 feeds
file_exists issue.
PHP Whos Online Code OR Page refresh..
URL Parameters PHP
removing DirectAdmin part of URL makes php code work
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.