View previous topic :: View next topic |
Author |
Message |
TerraFrost -
Joined: 17 Oct 2004 Posts: 4
|
Posted: Wed Oct 26, 2005 8:23 am Post subject: Mimcing Apache's mod_rewrite functionality |
|
|
I just installed MediaWiki and would like to do some "url rewritting" as described here. ie. I'd like to incorporate the functionality of the following .htaccess file into Abyss:
Code: | RewriteEngine on
RewriteRule ^wiki/?(.*)$ /w/index.php?title=$1 [L,QSA] |
Unfortunately, I'm not really sure how to do this. Any ideas? |
|
Back to top |
|
|
aprelium -
Joined: 22 Mar 2002 Posts: 6800
|
Posted: Wed Oct 26, 2005 2:13 pm Post subject: Re: Mimcing Apache's mod_rewrite functionality |
|
|
TerraFrost,
Here is a simple script in PHP that can do what you ask for. Call it for example 404.php and configure it to be your custom 404 error page.
Code: |
<?php
/* Add in this array the list of (old path => new path) pairs */
$redirection = array(
'^wiki/?(.*)$' => '/w/index.php?title=$1'
);
/* Get the URI and trim leading slashes */
$uri = ltrim($_SERVER["REDIRECT_SCRIPT_NAME"], "/");
foreach ($redirection as $key => $value)
{
if (eregi($key, $uri))
{
/* Convert the replacement string syntax - $1 -> \1 */
/* and perform the substitution */
$new_uri = eregi_replace($key, str_replace("$", "\\", $value), $uri);
break;
}
}
if (isset($new_uri))
{
header("Status: 307");
header("Location: $new_uri");
exit;
}
?>
<!-- Your 404 error page -->
<HTML>
<HEAD>
<TITLE>
Not Found
</TITLE>
</HEAD>
<BODY>
The object <tt><?php echo $uri; ?></tt> is not available.
</BODY>
</HTML>
|
All you have to is to customize the last part of the page and to fill the $redirection array with the old/new paths pairs (taken from your mod_rewrite settings).
If you're not using Windows, replace eregi with ereg and eregi_replace with ereg_replace (because only Windows is case insensitive with file names). _________________ Support Team
Aprelium - http://www.aprelium.com
Last edited by aprelium on Thu Oct 27, 2005 3:36 pm; edited 2 times in total |
|
Back to top |
|
|
TerraFrost -
Joined: 17 Oct 2004 Posts: 4
|
Posted: Thu Oct 27, 2005 12:59 am Post subject: |
|
|
Apart from the fact that isset should be used in the last if statement, the script works great - thanks! :) |
|
Back to top |
|
|
MonkeyNation -
Joined: 05 Feb 2005 Posts: 921 Location: Cardiff
|
Posted: Thu Oct 27, 2005 1:30 am Post subject: |
|
|
TerraFrost wrote: | Apart from the fact that isset should be used in the last if statement, the script works great - thanks! :) |
if (something) has the same functionality as if (isset(something)).
/pointless _________________
|
|
Back to top |
|
|
aprelium -
Joined: 22 Mar 2002 Posts: 6800
|
Posted: Thu Oct 27, 2005 3:35 pm Post subject: |
|
|
TerraFrost wrote: | Apart from the fact that isset should be used in the last if statement, the script works great - thanks! :) |
We have updated the script with isset as you suggested (MokeyNation, it will prevent people to get a notice from PHP).
We have also added a new header() statement to set explicitly the status code. With no status code, an internal redirection is done and the final displayed page will still have the status code 404 which can cause problems for people with Internet Explorer and its friendly error pages.
With the explicit status code set, 404 is superceded and people will get to the right page regardless of their browser settings.
Notice we've used the not very known status code 307 which is a variant of the 3xx status codes. It instructs the server to retry the request with the new URL which is equivalent to the behavior of 302 with GET. But it will also force the browser to resend any POSTed data to the new URL if the method is POST (which is not possible with other status codes).
You can also use the status code 301 instead of 307 if your pages do not contain forms. _________________ Support Team
Aprelium - http://www.aprelium.com |
|
Back to top |
|
|
MonkeyNation -
Joined: 05 Feb 2005 Posts: 921 Location: Cardiff
|
Posted: Thu Oct 27, 2005 10:10 pm Post subject: |
|
|
aprelium wrote: | MokeyNation, it will prevent people to get a notice from PHP |
I killed notices :x _________________
|
|
Back to top |
|
|
raderack -
Joined: 02 Sep 2003 Posts: 16
|
Posted: Wed Mar 08, 2006 5:53 pm Post subject: |
|
|
ok i have a image hosting script that need mod rewrite
i tried to used this hack
in the htacess the rule is this: RewriteRule ^([0-9]+)/(.+)$ ../file.php?userid=$1&file=$2 [L,NC]
so i adapted to the 404.php as this:
/* Add in this array the list of (old path => new path) pairs */
$redirection = array(
'^([0-9]+)/(.+)$' => '../file.php?userid=$1&file=$2 '
);
but still dont work. |
|
Back to top |
|
|
TRUSTAbyss -
Joined: 29 Oct 2003 Posts: 3752 Location: USA, GA
|
Posted: Wed Mar 08, 2006 11:44 pm Post subject: |
|
|
Can you tell us what Image Hosting script you are using so that we may
test this on our own servers? |
|
Back to top |
|
|
raderack -
Joined: 02 Sep 2003 Posts: 16
|
Posted: Thu Mar 09, 2006 12:54 am Post subject: |
|
|
Sure trustpunk
Uploaderv6.1 © www.celerondude.com
It works fine,but for the bandwidth limit function it needs mod_rewrite.
If you got that problem solved,please let me known.
Thanks
(tHe version 6.1 is free,but the version 6.2 you need to pay for it) |
|
Back to top |
|
|
TRUSTAbyss -
Joined: 29 Oct 2003 Posts: 3752 Location: USA, GA
|
Posted: Thu Mar 09, 2006 3:31 am Post subject: |
|
|
I'm sorry but I too have no clue how to get this to work. Maybe someone on
the forum can help with this, or even Aprelium may clear things up for you. :-)
I don't know how aprelium's script can do this one. It seems impossible. |
|
Back to top |
|
|
aprelium -
Joined: 22 Mar 2002 Posts: 6800
|
Posted: Thu Mar 09, 2006 10:47 am Post subject: |
|
|
raderack wrote: | ok i have a image hosting script that need mod rewrite
i tried to used this hack
in the htacess the rule is this: RewriteRule ^([0-9]+)/(.+)$ ../file.php?userid=$1&file=$2 [L,NC]
so i adapted to the 404.php as this:
/* Add in this array the list of (old path => new path) pairs */
$redirection = array(
'^([0-9]+)/(.+)$' => '../file.php?userid=$1&file=$2 '
);
but still dont work. |
Your mod_rewrite rule seems to be restricted to a given directory and not to the whole web site (because there is a reference to a relative path ../file.php.
Where is this rule supposed to be (in which virtual path)? _________________ Support Team
Aprelium - http://www.aprelium.com |
|
Back to top |
|
|
raderack -
Joined: 02 Sep 2003 Posts: 16
|
Posted: Fri Mar 10, 2006 7:02 am Post subject: |
|
|
in the files dir
im my case /hosting/files
/hosting-> main dir of the image upload script
aprelium wrote: | raderack wrote: | ok i have a image hosting script that need mod rewrite
i tried to used this hack
in the htacess the rule is this: RewriteRule ^([0-9]+)/(.+)$ ../file.php?userid=$1&file=$2 [L,NC]
so i adapted to the 404.php as this:
/* Add in this array the list of (old path => new path) pairs */
$redirection = array(
'^([0-9]+)/(.+)$' => '../file.php?userid=$1&file=$2 '
);
but still dont work. |
Your mod_rewrite rule seems to be restricted to a given directory and not to the whole web site (because there is a reference to a relative path ../file.php.
Where is this rule supposed to be (in which virtual path)? |
|
|
Back to top |
|
|
aprelium -
Joined: 22 Mar 2002 Posts: 6800
|
Posted: Fri Mar 10, 2006 11:35 am Post subject: |
|
|
raderack,
Try using the following rule:
Code: | /* Add in this array the list of (old path => new path) pairs */
$redirection = array(
'^hosting/files/([0-9]+)/(.+)$' => '/hosting/file.php?userid=$1&file=$2 '
); |
_________________ Support Team
Aprelium - http://www.aprelium.com |
|
Back to top |
|
|
munkywrench -
Joined: 04 May 2006 Posts: 2
|
Posted: Thu May 04, 2006 7:57 am Post subject: |
|
|
This doesn't seem to work on my abyss installation - $_SERVER['REDIRECT_SCRIPT_NAME'] is not set. Any ideas?
When I do print_r($_SERVER), there is no $_SERVER index called 'REDIRECT_SCRIPT_NAME'... I'd love to have it. Can anyone tell me where its gone? :( |
|
Back to top |
|
|
aprelium -
Joined: 22 Mar 2002 Posts: 6800
|
Posted: Thu May 04, 2006 9:54 am Post subject: |
|
|
munkywrench wrote: | This doesn't seem to work on my abyss installation - $_SERVER['REDIRECT_SCRIPT_NAME'] is not set. Any ideas?
When I do print_r($_SERVER), there is no $_SERVER index called 'REDIRECT_SCRIPT_NAME'... I'd love to have it. Can anyone tell me where its gone? :( |
REDIRECT_SCRIPT_NAME is not set unless you are running an error page script. When you browse directly the script, this variable is not set (because there was no internal redirection). _________________ Support Team
Aprelium - http://www.aprelium.com |
|
Back to top |
|
|
munkywrench -
Joined: 04 May 2006 Posts: 2
|
Posted: Fri May 05, 2006 3:03 am Post subject: |
|
|
I'm doing the print_r() call on my error page, after being redirected from http://localhost/blah/foo, and the variable doesn't exist |
|
Back to top |
|
|
aprelium -
Joined: 22 Mar 2002 Posts: 6800
|
Posted: Fri May 05, 2006 11:28 am Post subject: |
|
|
munkywrench wrote: | I'm doing the print_r() call on my error page, after being redirected from http://localhost/blah/foo, and the variable doesn't exist |
Add a phpinfo() call in your script and check all the available variables. Coud you post them here? (the variables list printed by phpinfo()) _________________ Support Team
Aprelium - http://www.aprelium.com |
|
Back to top |
|
|
loloyd -
Joined: 03 Mar 2006 Posts: 435 Location: Philippines
|
|
Back to top |
|
|
MonkeyNation -
Joined: 05 Feb 2005 Posts: 921 Location: Cardiff
|
Posted: Sun May 07, 2006 8:46 pm Post subject: |
|
|
This bit:
Code: | RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d |
Means only redirect if the requested URL isn't a file or directory.
Put an if (!file_exists($_SERVER["REDIRECT_SCRIPT_NAME"])) before the redirection. _________________
|
|
Back to top |
|
|
loloyd -
Joined: 03 Mar 2006 Posts: 435 Location: Philippines
|
Posted: Sun May 07, 2006 9:03 pm Post subject: Scope? |
|
|
Can I know before what line?
And when will I terminate the if-condition?
Also, is this not assumed as this goes in 404.php? _________________
http://home.loloyd.com/ is online if the logo graphic at left is showing. |
|
Back to top |
|
|
aprelium -
Joined: 22 Mar 2002 Posts: 6800
|
Posted: Mon May 08, 2006 9:56 am Post subject: |
|
|
MonkeyNation wrote: | This bit:
Code: | RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d |
Means only redirect if the requested URL isn't a file or directory.
Put an if (!file_exists($_SERVER["REDIRECT_SCRIPT_NAME"])) before the redirection. |
You can ignore these two lines as Abyss Web Server is using your 404 custom script because it found no file or directory with the requested name. _________________ Support Team
Aprelium - http://www.aprelium.com |
|
Back to top |
|
|
MonkeyNation -
Joined: 05 Feb 2005 Posts: 921 Location: Cardiff
|
Posted: Mon May 08, 2006 10:06 am Post subject: |
|
|
aprelium wrote: | You can ignore these two lines as Abyss Web Server is using your 404 custom script because it found no file or directory with the requested name. |
Well, it's what made the endless loop. A 404 error is more legible. _________________
|
|
Back to top |
|
|
loloyd -
Joined: 03 Mar 2006 Posts: 435 Location: Philippines
|
Posted: Thu May 25, 2006 5:34 pm Post subject: |
|
|
Hehehe, am I stupid or what?!?!
Since I was only simply trying to convert from
/one/two/three
to
/index.php?q=one/two/three
I should've thought of a simple concatenation in the first place!
From above, simply replacing
$new_uri = eregi_replace($key, str_replace("$", "\\", $value), $uri);
with
$new_uri = "/index.php?q=".$uri;
does the trick for this purpose. What was I thinking? How stupid was I then? Is there a rule here against flaming one's own self? Aaaarrggghhh!!!
Hindsight sure is 20/20. I can't believe these things still happen to me, it's so frustrating. *sigh*
Update: Clean URL Support in Abyss (for Drupal)
http://drupal.org/node/66114 _________________
http://home.loloyd.com/ is online if the logo graphic at left is showing. |
|
Back to top |
|
|
aprelium -
Joined: 22 Mar 2002 Posts: 6800
|
|
Back to top |
|
|
|