开发者

redirect all files with .php extension

have this code in a htaccess file to hide the final php extension of each file

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

but now i want to redirect to another page if the 开发者_开发知识库user try to complete the extension.

i tried this line after the script above but i get a 500 internal error

RewriteRule \.php$ - [R=404]

thanks !


Have your rewrite rules like this:

RewriteEngine on
Options +FollowSymlinks -MultiViews

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond  %{QUERY_STRING} !^myvar=0
RewriteCond %{REQUEST_URI} !\..*$
RewriteRule ^(.*)$ /$1.php?myvar=0 [QSA,L]

# but don't allow example.com/foo.php
RewriteCond  %{QUERY_STRING} !^myvar=0$
RewriteRule \.php$ - [R=404,L]

Basically first rule is appending a dummy query parameter myvar=0 in the URL and second is checking its non-presence to kick in, hence if user types /abc.php then it is blocked but if just /abc then it is internally redirected to /abc.php.


Mod_rewrite only accepts the values 301, and 302 (the default if not specified) for the R flag. If you wish to send to a 404, I'd recommend sending the request to a custom 404 page: RewriteRule \.php$ /404.php [L], where 404.php sets the Response Status to 404.


Apache Server Help Documentation: Apache 2.2 was released in 2005 - 10 years ago. https://en.wikipedia.org/wiki/Apache_HTTP_Server

Apache 2.2 R Flag HTTP response status code help info: https://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_r

Any valid HTTP response status code may be specified, using the syntax [R=305], with a 302 status code being used by default if none is specified. The status code specified need not necessarily be a redirect (3xx) status code. However, if a status code is outside the redirect range (300-399) then the substitution string is dropped entirely, and rewriting is stopped as if the L were used.

Apache 2.4 R Flag HTTP response status code help info: https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_r

Any valid HTTP response status code may be specified, using the syntax [R=305], with a 302 status code being used by default if none is specified. The status code specified need not necessarily be a redirect (3xx) status code. However, if a status code is outside the redirect range (300-399) then the substitution string is dropped entirely, and rewriting is stopped as if the L were used.

Additional relevant Info:

"The target (or substitution string) in a RewriteRule is assumed to be a file path, by default."

"- (dash) A dash indicates that no substitution should be performed (the existing path is passed through untouched). This is used when a flag (see below) needs to be applied without changing the path."

In plain english the substitution string is the http URI path were you are rewriting/redirecting too. So that means that when you use a 4xx redirect range, such as R=405, then a URI target path will be ignored/not allowed/not used if one is present in the RewriteRule.

This RewriteRule is valid and works in most cases/most servers/server configuration, but may not work in every single case so yep if you want something that works in 100% of all server configurations then you would dumb this code down and not use R=405. In Live testing R=405 works in 99.99% of all cases on hosts worldwide.

RewriteCond %{REQUEST_METHOD} ^(HEAD) [NC]
RewriteRule ^(.*)$ - [R=405,L]

Dumbed down code:

RewriteCond %{REQUEST_METHOD} ^(HEAD) [NC]
RewriteRule ^(.*)$ /path-to-405-error-logging-file/405.php [L]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜