Jump to content

URLs as variables to a PHP script


boyakasha

Recommended Posts

I want to set up a website such that I create a file, let's say [color=red]/something/redirector.php[/color], that receives any url in the [color=red]/something/[/color] directory with the requested URL as an argument.

For example, if the user requests [color=red]/something/action/variable[/color] then it calls [color=red]/something/redirector.php?path=/something/action/variable[/color] or something similar.

It doesn't have to use GET variables, I just need the requested URL to be available to [color=red]/something/redirector.php[/color]. I had a look around the web and happened across mod_rewrite for Apache, which may be able to do it, but I was wondering if there was an easier way that anyone knew of.
Link to comment
https://forums.phpfreaks.com/topic/34699-urls-as-variables-to-a-php-script/
Share on other sites

I want to set up a website such that I create a file, let's say /something/redirector.php, that receives any url in the /something/ directory with the requested URL as an argument.

For example, if the user requests /something/action/variable then it calls /something/redirector.php?path=/something/action/variable or something similar.

[b]/.htaccess[/b]
[code]
option +followsymlinks
RewriteEngine On
RewriteRule ^/something.*$ /something/redirector.php?path=$1
[/code]

I would imagine that rewrite rule would work. Don't hold me to it though ;).
Thanks for the replies. I had a better look at mod_rewrite and ended up "borrowing" settings from the ruby on rails .htaccess file. I ended up using:

[code]<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ index.php [QSA]
RewriteRule ^([^.]+)$ $1.php [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.php [QSA,L]
</IfModule>[/code]

Works like a charm. If a page doesn't exist, it gets redirected to dispatch.php and $_SERVER['REQUEST_URI'] is set to the requested URL.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.