daanoz Posted March 7, 2009 Share Posted March 7, 2009 Hey ppl, I've been working with dynamic urls on quite some projects now, and gotten quite used to it. To make clear what i do (because dynamic url handling might not be a good name), i'll give a short example : Say, you have a page with a list of projects, and you want to offer visitors a info + download page to each project. To make a url look nice, we can do a simple trick. so instead of: - http://localhost/projects.php?id=1 - http://localhost/projects.php?id=22 - http://localhost/projects.php?id=34 - http://localhost/projects.php?id=4&ac=download we get: - http://localhost/projects/SearhApp - http://localhost/projects/DownloadApplication - http://localhost/projects/AnotherApp - http://localhost/projects/SearchAppV2/Download The trick behind this is renaming the "projects.php" to "projects" and making apache handling files with no extension as a php file (make sure you secure your application properly otherwise this might pose a security risk) Right, my question... I want to take this a step further, but i have no clue how. I want to remove the "projects" file as well. So I can have links like: - http://localhost/SearhApp - http://localhost/DownloadApplication - http://localhost/AnotherApp - http://localhost/SearchAppV2/Download Without having actually having to create the files, i want to call the same php script everytime a url is requested. I think the same technique is used at www.php.net. Behind the domain you can put any php function your looking for, and you get redirected to the page you want (http://www.php.net/array_key_exists, http://www.php.net/fread, etc.) So... how is it done? Cheers Daanoz Quote Link to comment https://forums.phpfreaks.com/topic/148382-solved-dynamic-url-handling-at-root-level/ Share on other sites More sharing options...
wildteen88 Posted March 7, 2009 Share Posted March 7, 2009 If you're using Apache as your HTTP server you can use mod_rewrite to achieve this, example RewriteEngine On RewriteRule /([a-z0-9]+)/ /projects/$1/ [NC,L] With that within a .htaccess file any url like http://locahost/xyz/ will actually call http://localhost/projects/xyz/ Quote Link to comment https://forums.phpfreaks.com/topic/148382-solved-dynamic-url-handling-at-root-level/#findComment-779035 Share on other sites More sharing options...
daanoz Posted March 7, 2009 Author Share Posted March 7, 2009 Ok, i think i'm on the right track, but i'm missing something... RewriteEngine On RewriteRule /([a-z0-9]+) /testsite/httpdocs/projects/$1/ [NC,L] results in: [sat Mar 07 20:21:49 2009] [error] [client 127.0.0.1] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace. [sat Mar 07 20:21:49 2009] [debug] core.c(3046): [client 127.0.0.1] r->uri = /testsite/httpdocs/projects/1/ [sat Mar 07 20:21:49 2009] [debug] core.c(3052): [client 127.0.0.1] redirected from r->uri = /testsite/httpdocs/projects/1/ [sat Mar 07 20:21:49 2009] [debug] core.c(3052): [client 127.0.0.1] redirected from r->uri = /testsite/httpdocs/projects/1/ [sat Mar 07 20:21:49 2009] [debug] core.c(3052): [client 127.0.0.1] redirected from r->uri = /testsite/httpdocs/projects/1/ [sat Mar 07 20:21:49 2009] [debug] core.c(3052): [client 127.0.0.1] redirected from r->uri = /testsite/httpdocs/projects/1/ [sat Mar 07 20:21:49 2009] [debug] core.c(3052): [client 127.0.0.1] redirected from r->uri = /testsite/httpdocs/projects/1/ [sat Mar 07 20:21:49 2009] [debug] core.c(3052): [client 127.0.0.1] redirected from r->uri = /testsite/httpdocs/projects/1/ [sat Mar 07 20:21:49 2009] [debug] core.c(3052): [client 127.0.0.1] redirected from r->uri = /testsite/httpdocs/projects/1/ [sat Mar 07 20:21:49 2009] [debug] core.c(3052): [client 127.0.0.1] redirected from r->uri = /testsite/httpdocs/projects/1/ [sat Mar 07 20:21:49 2009] [debug] core.c(3052): [client 127.0.0.1] redirected from r->uri = /testsite/httpdocs/projects/1/ [sat Mar 07 20:21:49 2009] [debug] core.c(3052): [client 127.0.0.1] redirected from r->uri = /testsite/httpdocs/test/1 whenever i call: http://localhost/testsite/httpdocs/test/1 my guess is that it also rewrites files that do exists (so i loops in a circle) is there a good way to overcome this? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/148382-solved-dynamic-url-handling-at-root-level/#findComment-779064 Share on other sites More sharing options...
wildteen88 Posted March 7, 2009 Share Posted March 7, 2009 Change the mod_rewrite code to RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule /([a-z0-9]+) /testsite/httpdocs/projects/$1/ [NC,L] Quote Link to comment https://forums.phpfreaks.com/topic/148382-solved-dynamic-url-handling-at-root-level/#findComment-779083 Share on other sites More sharing options...
daanoz Posted March 7, 2009 Author Share Posted March 7, 2009 works exactly as i meant, thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/148382-solved-dynamic-url-handling-at-root-level/#findComment-779087 Share on other sites More sharing options...
corbin Posted March 7, 2009 Share Posted March 7, 2009 Hrmmm I think that could still potentially make an infinite loop if a non existing URL were put in. Or would the directory flag stop it if the parent directory existed? Quote Link to comment https://forums.phpfreaks.com/topic/148382-solved-dynamic-url-handling-at-root-level/#findComment-779131 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.