Click to See Complete Forum and Search --> : help with regular expressions in .htaccess files
marcnyc
10-13-2004, 07:56 PM
Hello I am not familiar enough with RegExp and with .htaccess to do this so I was wondering if somebody wouldn't mind writing me the line of code that I need to redirect a file with php3 extension to the same file with php extension. Basically I'd like the user who goes to this.php3 to be redirected to this.php, the user who goes to that.php3 to be redirected to that.php and so on...
Thanks for your help.
Weedpacket
10-14-2004, 06:47 AM
First of all, what have you learned from reading the Apache documentation? I ask this in case someone suggests something in there that you've already tried.
marcnyc
10-14-2004, 11:03 AM
To be honest I never read the Apach documentation but on your hint I went to apache.org and went through the part related to .htaccess at http://httpd.apache.org/docs-2.1/howto/htaccess.html but it makes no mention of RegExp
Look through the documentation for mod_rewrite (http://httpd.apache.org/docs/mod/mod_rewrite.html) and it will.
laserlight
10-14-2004, 11:13 AM
Look at the docs for mod_rewrite (http://httpd.apache.org/docs-2.1/mod/mod_rewrite.html)
You could also take a look at Using Regular Expressions (http://etext.lib.virginia.edu/helpsheets/regex.html), by Steve Ramsay.
marcnyc
10-14-2004, 02:03 PM
Ok I looked at Ramsay's document and came up with something that I guess must be wrong because I get a 500 Internal Server Error, no matter what document I try to open:
Redirect .*\.php? \1.php
I was hoping to match any number characters before the dot (which I escaped) and the letters p, h and p as well as any or zero instances of any other character (so that files with extension php3, php and php4 are all matched). Then I was hoping to redirect the user to that same document (backreference \1 of the matched value) but with extension php.
I also tried: Redirect .*\.php? \1\.php
same thing
Weedpacket
10-15-2004, 03:29 AM
On the mod_rewrite page I found the example
RewriteRule ^(.+\.php)s$ $1 which rewrites files with names ending with ".phps" to end instead with ".php" (the idea was that these would then be processed and rendered as syntax-highlighted source code).
So to turn that into what you want, it's a matter of replacing the 's' with "any digit". The most conventional way of doing that would be to write '[0-9]'; as the page also mentions that Apache uses Perl-compatible regexps, it could be written as '\d' instead.
marcnyc
10-18-2004, 09:48 AM
I tried both:
RewriteRule ^(.+\.php)[0-9]$ $1
and
RewriteRule ^(.+\.php)\d$ $1
It doesn't work. Did I miss something?
PHP Builder
Copyright WebMediaBrands Inc. All Rights Reserved.