简体   繁体   English

Linux CentOS 7-通过.htaccess配置httpd.conf文件

[英]Linux CentOS 7 - Configuration of httpd.conf file through .htaccess

  1. I need to configure through .htaccess in Linux CentOS 7 that my info.php file is shown without extension. 我需要通过Linux CentOS 7中的.htaccess进行配置,以使我的info.php文件显示为不带扩展名。 That is, calling http://server address/info instead of calling http://server address/info.php . 也就是说,调用http://server address/info而不是调用http://server address/info.php

  2. Also, in .htaccess , add that phpinformation redirects to info . 另外,在.htaccess ,添加phpinformation重定向到info That is, http://server address/phpinformation redirects to http://server address/info . 也就是说, http://server address/phpinformation重定向到http://server address/info

I have followed the part of the following article . 我已经关注了以下文章的一部分。

In the httpd.conf file, I have changed AllowOverride none to AllowOverride AuthConfig . httpd.conf文件中,我将AllowOverride none更改为AllowOverride AuthConfig

What are the next steps? 什么是下一个步骤?

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) 要重写你的文件,你应该overrite FileInfo的类型指令,而不是AuthConfig(请参https://httpd.apache.org/docs/2.4/mod/core.html#allowoverride为参考)

Enable the mod_rewrite module in the apache configuration and in the .htaccess file use a similar configuration: 在apache配置中启用mod_rewrite模块,并在.htaccess文件中使用类似的配置:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^phpinformation$ info.php [L]
    RewriteRule ^info$ info.php [L]
</IfModule>

A more general configuration: 更一般的配置:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^phpinformation$ info.php [L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ $1.php [L]
</IfModule>

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. 第一个重写规则是相同的,如果您必须使用仅包含phpinformation的URL服务请求,则将其重写为info.php并改为提供该URL。 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. L修饰符意味着重写器不需要搜索其他重写,而可以要求apache服务于生成的(info.php)规则。

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). 在这种情况下,原始URL不会解析为现有文件(!-f)或目录(!-d)。 If the file/directory exists it will be served as usual 如果文件/目录存在,将照常提供

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: 您可能还需要执行外部重定向,以强制客户端访问资源的正式URL,在这种情况下,第一个示例可以进行以下更改:

RedirectMatch ^/phpinformation$ /info
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^info$ info.php [L]
</IfModule>

Why do not use the RedirectMatch for both the urls? 为什么不对两个URL都使用RedirectMatch? 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. 原因是客户端用户在浏览器上看到重定向的URL,因此我们需要删除的.php后缀会再次弹出。

References 参考

Mod_rewrite documentation Mod_rewrite文档
AllowOverride documentation AllowOverride文档
RedirectMatch documentation RedirectMatch文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM