我需要通过.htaccess中的Linux CentOS 7配置我的info.php文件显示时没有扩展名。也就是说,调用http://server address/info而不是调用http://server address/info.php
另外,在.htaccess中,添加phpinformation重定向到info。也就是说,http://server address/phpinformation重定向到http://server address/info
我已经遵循了以下article部分。
httpd.conf文件中,我已将AllowOverride none更改为AllowOverride AuthConfig
接下来的步骤是什么?

最佳答案

要重写文件,应该重写指令的FileInfo类型,而不是AuthConfig类型(有关引用,请参见https://httpd.apache.org/docs/2.4/mod/core.html#allowoverride
在apache配置中启用mod_rewrite模块
在.htaccess文件中使用类似的配置:

<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)规则。
第二个规则有点不同,重写引擎只在满足所有先前条件的情况下执行重写。在这种情况下,原始URL不能解析为现有文件(!-f)或目录(!-d)。
如果存在文件/目录,它将按常规服务。
您可能还希望执行外部重定向,以强制客户端访问资源的官方url,在这种情况下,第一个示例可以在类似的情况下更改:
RedirectMatch ^/phpinformation$ /info
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^info$ info.php [L]
</IfModule>

为什么不对这两个url使用RedirectMatch?原因是客户端用户在浏览器上看到重定向的url,因此需要去掉的.php后缀会再次弹出。
工具书类
Mod_rewrite documentation
AllowOverride documentation
RedirectMatch documentation

关于php - Linux CentOS 7-通过.htaccess配置httpd.conf文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48984999/

10-12 12:28
查看更多