Welcome to WebmasterWorld Guest from 3.92.92.168

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

RewriteRule equivalent to AliasMatch

Using RewriteRule to reproduce the effects of an AliasMatch directive

     
3:30 pm on Nov 29, 2003 (gmt 0)

New User

10+ Year Member

joined:Nov 29, 2003
posts:3
votes: 0


I've got a PHP-based website that uses an AliasMatch directive in the httpd.conf file to send all requests through one PHP file as follows:

AliasMatch ^(/.+)$ /var/www/index.php$1

This works fine, but I now need to have the same effect from a RewriteRule in a .htaccess file (as I don't have access to the httpd.conf file on the new server).

I've had a rummage through the forums here (and delved through Google for a couple of hours) but not come up with a workable solution.

I basically need the following to happen:

1) User visits ht*p://www.doman.com/products/thing

2) Apache redirects (internally) to /index.php/products/thing

3) PHP then uses the PATH_INFO variable (which *should* contain /products/thing) to work out what to display

Any suggestions welcomed...

5:49 pm on Nov 29, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Barti,

Welcome to WebmasterWorld [webmasterworld.com]!

That might be simple, or it might be hard, depending on mod_rewrite's behaviour with PATH_INFO - I'm not sure what it does with that variable during a rewrite.

All you need to do is rewrite the filename and try it to see if PATH_INFO is correct. The one thing you do need to be aware of is that the path available for matching in RewriteRule in an .htaccess context is different from the path in httpd.conf, in that the leading slash will be stripped off in .htaccess context.

So it's basically just


RewriteBase /var/www
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule (.*) /index.php/$1 [L]

The only difference being the slash following "php" and the use of RewriteBase to specify the physical directory. You may have to tweak RewriteBase to match your new context; If it doesn't work, look in your raw error log file and you'll probably be able to see what the physical path needs to be. If that doesn't help, you can just use the method you showed above, and include it directly in the rewrite.

Jim

6:28 pm on Nov 29, 2003 (gmt 0)

New User

10+ Year Member

joined:Nov 29, 2003
posts:3
votes: 0


So it's basically just

RewriteBase /var/www
RewriteCond %{REQUEST_URI}!^/index\.php
RewriteRule (.*) /index.php/$1 [L]

Thanks for the above - it wasn't quite what was needed, but did point me in the right direction...

The above code wasn't putting the right value in PATH_INFO, but this one does:

RewriteRule (.+) /index.php%{REQUEST_URI} [L]

It simply sticks the entire of the REQUEST_URI onto the end of the new destination (ie everything after the end of the domain).

Cheers, and thanks for the very quick response...

Steve

7:56 pm on Nov 29, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Barti,

That's interesting - the two RewriteRules should have been equivalent.

I'd be interested to know what the PATH_INFO was with each version, if you have time to post it.

If you're not going to use the $1 backrefernce, you can shorten it up a bit:


RewriteRule . /index.php%{REQUEST_URI} [L]

Jim
8:39 pm on Nov 29, 2003 (gmt 0)

New User

10+ Year Member

joined:Nov 29, 2003
posts:3
votes: 0


I'd be interested to know what the PATH_INFO was with each version, if you have time to post it.

Here's the relevant values given by my version (using the $_SERVER array from within PHP 4.3 under Apache/1.3.26):


[QUERY_STRING] =>
[REQUEST_URI] => /
[SCRIPT_NAME] => /index.php
[PATH_INFO] => /index

...and the original:


[QUERY_STRING] =>
[REQUEST_URI] => /
[SCRIPT_NAME] => /index.php
[PATH_INFO] => /

I've not changed anything other than in the .htaccess files to get the different result. Puzzling yes?

Steve

8:53 pm on Nov 29, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Steve,

That's interesting... The $1 back-reference version apparently did not do a subrequest to resolve PATH_INFO "/" to "index.php". The different behaviour could be useful... One more entry for the notebook!

Thanks,
Jim