Fine-tuning a rewrite rule
am 07.11.2007 14:49:06 von landemaine
Hello,
I just wrote a few rules for my web site:
RewriteEngine on
# This rule transforms user.php?name=bob to /user/bob
# The [L] directive stops processing further rules if pattern matches
RewriteRule ^user/([^/\.]+)/?$ user.php?name=$1 [L]
# This rule removes the .php extension
# So: search.php?a=b&c=d&e=f becomes search?a=b&c=d&e=f
# It's executed only if the previous rule didn't match
RewriteRule ^([^\.\?]+)(\?.*)?$ $1.php
ErrorDocument 404 /error-404/
Almost everything works great, except the custom error message rule
that seems ignored. If I access any non-existing page, I get the
default Apache 404 error page. I tried the following as well with no
success:
ErrorDocument 404 error-404.php
ErrorDocument 404 error-404
I do have an error-404.php file in the same directory as my .htaccess
file.
Other than that, I'd like that everytime some one types a file name
with the .php extension in his browser, he's redirected to the 404
error page, as if there weren't such file. This would be to hide the
technology. How could I do it with the above rules?
Thanks,
Charles.
Re: Fine-tuning a rewrite rule
am 08.11.2007 07:34:45 von Mr Apache
ErrorDocument 404 /error-404.php #you need absolute path if its not
an external
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /error-404.php [L]
See also: http://www.askapache.com/htaccess/ultimate-htaccess-file-sam ple.html
On Nov 7, 8:49 am, Charles wrote:
> Hello,
>
> I just wrote a few rules for my web site:
>
> RewriteEngine on
>
> # This rule transforms user.php?name=bob to /user/bob
> # The [L] directive stops processing further rules if pattern matches
>
> RewriteRule ^user/([^/\.]+)/?$ user.php?name=$1 [L]
>
> # This rule removes the .php extension
> # So: search.php?a=b&c=d&e=f becomes search?a=b&c=d&e=f
> # It's executed only if the previous rule didn't match
>
> RewriteRule ^([^\.\?]+)(\?.*)?$ $1.php
>
> ErrorDocument 404 /error-404/
>
> Almost everything works great, except the custom error message rule
> that seems ignored. If I access any non-existing page, I get the
> default Apache 404 error page. I tried the following as well with no
> success:
>
> ErrorDocument 404 error-404.php
> ErrorDocument 404 error-404
>
> I do have an error-404.php file in the same directory as my .htaccess
> file.
>
> Other than that, I'd like that everytime some one types a file name
> with the .php extension in his browser, he's redirected to the 404
> error page, as if there weren't such file. This would be to hide the
> technology. How could I do it with the above rules?
> Thanks,
>
> Charles.
Re: Fine-tuning a rewrite rule
am 09.11.2007 01:50:13 von landemaine
On 8 nov, 03:34, Apache Admin wrote:
> ErrorDocument 404 /error-404.php #you need absolute path if its not
> an external
>
> RewriteCond %{REQUEST_FILENAME} !-f
> RewriteCond %{REQUEST_FILENAME} !-d
> RewriteRule .* /error-404.php [L]
>
> See also:http://www.askapache.com/htaccess/ultimate-htaccess-fil e-sample.html
Thanks! This is actually the tutorial I found on the web yesterday :)
It's working fine now. I was wondering... Are the following lines
necessary? I'm asking because it works fine without, right?
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Thanks,
Charles.