Linux CentOS 7-通过.htaccess配置httpd.conf文件 [英] Linux CentOS 7 - Configuration of httpd.conf file through .htaccess
问题描述
-
我需要通过
Linux CentOS 7
中的.htaccess
进行配置,以显示我的info.php
文件没有扩展名.也就是说,调用http://服务器地址/信息
,而不是调用http://服务器地址/info.php
.
I need to configure through
.htaccess
inLinux CentOS 7
that myinfo.php
file is shown without extension. That is, callinghttp://server address/info
instead of callinghttp://server address/info.php
.
此外,在 .htaccess
中,添加 phpinformation
重定向到 info
.即, http://服务器地址/php信息
重定向到 http://服务器地址/信息
.
Also, in .htaccess
, add that phpinformation
redirects to info
. That is, http://server address/phpinformation
redirects to http://server address/info
.
我已遵循以下在 httpd.conf
文件中,我将 AllowOverride none
更改为 AllowOverride AuthConfig
.
In the httpd.conf
file, I have changed AllowOverride none
to AllowOverride AuthConfig
.
下一步是什么?
推荐答案
要重写文件,您应该覆盖 FileInfo 类型的指令,而不是 AuthConfig 类型的指令(请参见 https://httpd.apache.org/docs/2.4/mod/core.html#allowoverride 供参考)
To rewrite your files you should overrite the FileInfo type of directive, not the AuthConfig ones (see https://httpd.apache.org/docs/2.4/mod/core.html#allowoverride for references)
在apache配置中启用 mod_rewrite 模块并在.htaccess文件中使用类似的配置:
Enable the mod_rewrite module in the apache configuration and in the .htaccess file use a similar configuration:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^phpinformation$ info.php [L]
RewriteRule ^info$ info.php [L]
</IfModule>
更一般的配置:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^phpinformation$ info.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]
</IfModule>
第一个重写规则是相同的,如果您必须使用仅包含phpinformation的url服务请求,请将其重写为info.php并提供该url.L修饰符意味着重写器无需搜索其他重写,而只需请求apache服务即可生成结果(info.php)规则.
The first rewrite rule is the same, if you have to serve a request with an url containing just phpinformation, rewrite it to info.php and serve that url instead. The L modifier means that the rewriter does not need to search for additional rewriting and can just ask apache to serve the resulting (info.php) rule.
第二条规则有些不同,重写引擎仅在满足所有先前条件的情况下才执行重写.在这种情况下,原始URL不会解析为现有文件(!-f)或目录(!-d).如果文件/目录存在,将照常提供
The second rule is a bit different, the rewrite engine perform the rewrite only if ALL the previuous condition are met. In this case the original url does not resolve to an existing file (!-f) or directory (!-d). If the file/directory exists it will be served as usual
您可能还想执行外部重定向,以强制客户端访问资源的正式URL,在这种情况下,第一个示例可以进行以下更改:
You may also want to perform an external redirect to force the client to access to an official url for a resource, in this case the first example can change in something similar:
RedirectMatch ^/phpinformation$ /info
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^info$ info.php [L]
</IfModule>
为什么两个URL都不使用RedirectMatch?原因是客户端用户在浏览器上看到了重定向的URL,因此我们需要删除的.php后缀会再次弹出.
Why do not use the RedirectMatch for both the urls? The reason is that the client user sees the redirected url on the browser and thus the .php suffix, we need to rid off, would pop up again.
Mod_rewrite文档
AllowOverride文档
重定向匹配文档
这篇关于Linux CentOS 7-通过.htaccess配置httpd.conf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!