重定向所有带有 .php 扩展名的文件

发布于 2024-10-30 19:54:53 字数 332 浏览 1 评论 0原文

将此代码放在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久而酒知 2024-11-06 19:54:53

重写规则如下:

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]

基本上,第一条规则是在 URL 中附加一个虚拟查询参数 myvar=0,第二条规则是检查其不存在以启动,因此如果用户输入 /abc.php,则它被阻止,但如果只是 /abc 那么它会在内部重定向到 /abc.php。

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.

夜清冷一曲。 2024-11-06 19:54:53

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

可以使用以下语法指定任何有效的 HTTP 响应状态代码
[R=305],如果没有状态代码,则默认使用 302 状态代码
指定的。指定的状态码不一定是
重定向 (3xx) 状态代码。但是,如果状态代码超出范围
重定向范围 (300-399) 然后替换字符串被删除
完全,重写停止,就像使用了 L 一样。

Apache 2.4 R 标志 HTTP 响应状态代码帮助信息:
https://httpd.apache.org/docs/2.4/rewrite/flags .html#flag_r

可以使用以下语法指定任何有效的 HTTP 响应状态代码
[R=305],如果没有状态代码,则默认使用 302 状态代码
指定的。指定的状态码不一定是
重定向 (3xx) 状态代码。但是,如果状态代码超出范围
重定向范围 (300-399) 然后替换字符串被删除
完全,重写停止,就像使用了 L 一样。

其他相关信息:

“RewriteRule 中的目标(或替换字符串)假定为
默认情况下,文件路径。"

"-(破折号)
破折号表示不应执行替换(现有路径不受影响地通过)。当需要应用标志(见下文)而不更改路径时,将使用此选项。”

用简单的英语来说,替换字符串是您正在重写/重定向的 http URI 路径。因此,这意味着当您使用 4xx 重定向范围时,例如 R=405,则如果 RewriteRule 中存在 URI 目标路径,则该 URI 目标路径将被忽略/不允许/不使用。

此 RewriteRule 有效且适用于大多数情况/大多数服务器/服务器配置,但可能不适用于以下情况。 的,如果您想要在 100% 的所有服务器配置中工作,那么您可以简化此代码,而不是使用 R=405。在实时测试中,R=405 可在全球 99.99% 的主机上工作。

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

是 下代码:

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

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]
江心雾 2024-11-06 19:54:53

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文