重定向所有带有 .php 扩展名的文件
将此代码放在 htaccess 文件中以隐藏每个文件的最终 php 扩展名
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
,但现在如果用户尝试完成扩展名,我想重定向到另一个页面。
我在上面的脚本之后尝试了这一行,但我收到 500 内部错误,
RewriteRule \.php$ - [R=404]
谢谢!
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 !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
重写规则如下:
基本上,第一条规则是在 URL 中附加一个虚拟查询参数
myvar=0
,第二条规则是检查其不存在以启动,因此如果用户输入 /abc.php,则它被阻止,但如果只是 /abc 那么它会在内部重定向到 /abc.php。Have your rewrite rules like this:
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.Apache服务器帮助文档:
Apache 2.2 发布于 2005 年 - 10 年前。
https://en.wikipedia.org/wiki/Apache_HTTP_Server
Apache 2.2 R 标记 HTTP 响应状态代码帮助信息:
https://httpd.apache.org/docs/2.2/rewrite/flags .html#flag_r
Apache 2.4 R 标志 HTTP 响应状态代码帮助信息:
https://httpd.apache.org/docs/2.4/rewrite/flags .html#flag_r
其他相关信息:
“RewriteRule 中的目标(或替换字符串)假定为
默认情况下,文件路径。"
"-(破折号)
破折号表示不应执行替换(现有路径不受影响地通过)。当需要应用标志(见下文)而不更改路径时,将使用此选项。”
用简单的英语来说,替换字符串是您正在重写/重定向的 http URI 路径。因此,这意味着当您使用 4xx 重定向范围时,例如 R=405,则如果 RewriteRule 中存在 URI 目标路径,则该 URI 目标路径将被忽略/不允许/不使用。
此 RewriteRule 有效且适用于大多数情况/大多数服务器/服务器配置,但可能不适用于以下情况。 的,如果您想要在 100% 的所有服务器配置中工作,那么您可以简化此代码,而不是使用 R=405。在实时测试中,R=405 可在全球 99.99% 的主机上工作。
是 下代码:
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
Apache 2.4 R Flag HTTP response status code help info:
https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_r
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.
Dumbed down code:
Mod_rewrite 仅接受
R
标志的值 301 和 302(如果未指定,则为默认值)。如果您希望发送到 404,我建议将请求发送到自定义 404 页面:RewriteRule \.php$ /404.php [L]
,其中 404.php 设置响应状态到 404。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.