Mimcing Apache's mod_rewrite functionality

 
Post new topic   Reply to topic    Aprelium Forum Index -> General Questions
View previous topic :: View next topic  
Author Message
TerraFrost
-


Joined: 17 Oct 2004
Posts: 4

PostPosted: Wed Oct 26, 2005 8:23 am    Post subject: Mimcing Apache's mod_rewrite functionality Reply with quote

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 View user's profile Send private message
aprelium
-


Joined: 22 Mar 2002
Posts: 6800

PostPosted: Wed Oct 26, 2005 2:13 pm    Post subject: Re: Mimcing Apache's mod_rewrite functionality Reply with quote

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 View user's profile Send private message Send e-mail
TerraFrost
-


Joined: 17 Oct 2004
Posts: 4

PostPosted: Thu Oct 27, 2005 12:59 am    Post subject: Reply with quote

Apart from the fact that isset should be used in the last if statement, the script works great - thanks! :)
Back to top View user's profile Send private message
MonkeyNation
-


Joined: 05 Feb 2005
Posts: 921
Location: Cardiff

PostPosted: Thu Oct 27, 2005 1:30 am    Post subject: Reply with quote

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 View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger ICQ Number
aprelium
-


Joined: 22 Mar 2002
Posts: 6800

PostPosted: Thu Oct 27, 2005 3:35 pm    Post subject: Reply with quote

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 View user's profile Send private message Send e-mail
MonkeyNation
-


Joined: 05 Feb 2005
Posts: 921
Location: Cardiff

PostPosted: Thu Oct 27, 2005 10:10 pm    Post subject: Reply with quote

aprelium wrote:
MokeyNation, it will prevent people to get a notice from PHP


I killed notices :x
_________________
Back to top View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger ICQ Number
raderack
-


Joined: 02 Sep 2003
Posts: 16

PostPosted: Wed Mar 08, 2006 5:53 pm    Post subject: Reply with quote

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 View user's profile Send private message
TRUSTAbyss
-


Joined: 29 Oct 2003
Posts: 3752
Location: USA, GA

PostPosted: Wed Mar 08, 2006 11:44 pm    Post subject: Reply with quote

Can you tell us what Image Hosting script you are using so that we may
test this on our own servers?
Back to top View user's profile Send private message Visit poster's website
raderack
-


Joined: 02 Sep 2003
Posts: 16

PostPosted: Thu Mar 09, 2006 12:54 am    Post subject: Reply with quote

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 View user's profile Send private message
TRUSTAbyss
-


Joined: 29 Oct 2003
Posts: 3752
Location: USA, GA

PostPosted: Thu Mar 09, 2006 3:31 am    Post subject: Reply with quote

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 View user's profile Send private message Visit poster's website
aprelium
-


Joined: 22 Mar 2002
Posts: 6800

PostPosted: Thu Mar 09, 2006 10:47 am    Post subject: Reply with quote

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 View user's profile Send private message Send e-mail
raderack
-


Joined: 02 Sep 2003
Posts: 16

PostPosted: Fri Mar 10, 2006 7:02 am    Post subject: Reply with quote

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 View user's profile Send private message
aprelium
-


Joined: 22 Mar 2002
Posts: 6800

PostPosted: Fri Mar 10, 2006 11:35 am    Post subject: Reply with quote

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 View user's profile Send private message Send e-mail
munkywrench
-


Joined: 04 May 2006
Posts: 2

PostPosted: Thu May 04, 2006 7:57 am    Post subject: Reply with quote

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 View user's profile Send private message Visit poster's website
aprelium
-


Joined: 22 Mar 2002
Posts: 6800

PostPosted: Thu May 04, 2006 9:54 am    Post subject: Reply with quote

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 View user's profile Send private message Send e-mail
munkywrench
-


Joined: 04 May 2006
Posts: 2

PostPosted: Fri May 05, 2006 3:03 am    Post subject: Reply with quote

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 View user's profile Send private message Visit poster's website
aprelium
-


Joined: 22 Mar 2002
Posts: 6800

PostPosted: Fri May 05, 2006 11:28 am    Post subject: Reply with quote

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 View user's profile Send private message Send e-mail
loloyd
-


Joined: 03 Mar 2006
Posts: 435
Location: Philippines

PostPosted: Sun May 07, 2006 7:38 pm    Post subject: Drupal's URL Rewrite Reply with quote

For some reason, I can't get this rewrite to work in my Drupal installation. When I tested this in Firefox on a top directory resource, it works fine:

http://loloyd.homeip.net/ld_text_file_monitor
successfully redirected to
http://loloyd.homeip.net/index.php?q=ld_text_file_monitor

But when doing this in Firefox on a 2nd-level directory resource, it goes haywire:

http://loloyd.homeip.net/ld_text_file_monitor/downloads
gets erroneously redirected to
http://loloyd.homeip.net/ld_text_file_monitor/index.php?q=ld_text_file_monitor/index.php
and Firefox adds "Problem loading page: The page isn't redirecting properly. Firefox has detected that the server is redirecting the request for this address in a way that will never complete. * This problem can sometimes be caused by disabling or refusing to accept cookies."
The redirection I was aiming for was
http://loloyd.homeip.net/index.php?q=ld_text_file_monitor/downloads

It also runs amok on a top-level directory request on IE 6:

http://loloyd.homeip.net/ld_text_file_monitor/
has been lost, no clues on how the 307 redirection is taking place on IE 6 (poor HTTP/1.x compliance? never a surprise in Microsoft), page just keeps loading and loading and loading ad nauseum

My .htaccess looks like this:
Code:
# Rewrite current-style URLs of the form 'index.php?q=x'.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]


My redirection array in my 404.php looks this way:
Code:
$redirection = array(
   '^(.*)$' => 'index.php?q=$1'
);


I have copied Aprelium's PHP redirection script, replacing only the redirection array as stated herein. What can I do to correct these problems? I hope somebody can help enlighten me on this. Thanks...
_________________

http://home.loloyd.com/ is online if the logo graphic at left is showing.
Back to top View user's profile Send private message Visit poster's website
MonkeyNation
-


Joined: 05 Feb 2005
Posts: 921
Location: Cardiff

PostPosted: Sun May 07, 2006 8:46 pm    Post subject: Reply with quote

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 View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger ICQ Number
loloyd
-


Joined: 03 Mar 2006
Posts: 435
Location: Philippines

PostPosted: Sun May 07, 2006 9:03 pm    Post subject: Scope? Reply with quote

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 View user's profile Send private message Visit poster's website
aprelium
-


Joined: 22 Mar 2002
Posts: 6800

PostPosted: Mon May 08, 2006 9:56 am    Post subject: Reply with quote

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 View user's profile Send private message Send e-mail
MonkeyNation
-


Joined: 05 Feb 2005
Posts: 921
Location: Cardiff

PostPosted: Mon May 08, 2006 10:06 am    Post subject: Reply with quote

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 View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger ICQ Number
loloyd
-


Joined: 03 Mar 2006
Posts: 435
Location: Philippines

PostPosted: Thu May 25, 2006 5:34 pm    Post subject: Reply with quote

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 View user's profile Send private message Visit poster's website
aprelium
-


Joined: 22 Mar 2002
Posts: 6800

PostPosted: Fri Nov 24, 2006 2:43 pm    Post subject: Reply with quote

Version 2.4 supports now URL Rewriting. Its Beta release is available from http://www.aprelium.com/forum/viewforum.php?f=31 .
_________________
Support Team
Aprelium - http://www.aprelium.com
Back to top View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> General Questions All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group