我如何配置 Apache 以将文件名显示为目录?
How could I configure Apache to display file names as directories instead?
例如,我宁愿 CreateAccount.php
在客户看来显示为 http://www.example.com/CreateAccount/
。我知道创建很多目录并用 index.php
填充每个目录很容易,但我宁愿做最有效的事情。首先,使用该方法需要高度无组织且不必要地复杂的组织标准。其次,我也宁愿充分利用 Apache 的特性,即 mod_rewrite
.
我知道有 mod_rewrite
。我开始阅读该模块的文档,但我很快变得 非常 困惑和有点沮丧,因为我有注意力缺陷多动症,所以当我知道自己没有注意力时很难坐下来阅读看不懂我在读什么。
你能帮我找到最好的方法吗?
接近 mod_rewrite 的最佳方法是创建一个测试 server/virtual 主机并慢慢构建您的配置。另外,尽量让你的初始规则非常简单,这样你就可以准确地理解每条规则的作用。此配置可以帮助您开始您的流程。最后一条规则是您需要将 CreateAccount/ 路径映射到 CreateAccount.php 脚本。
<IfModule mod_rewrite.c>
RewriteEngine On
# You can use this only inside of the Directory directive or in the .htaccess file.
RewriteBase /
# http://www.example.com/page.html to redirect to http://www.example.com/page.php
RewriteRule ^(.*).html /.php [L]
# http://www.example.com/mypath/page.html to redirect to http://www.example.com/mypath/page.php
RewriteRule ^mypath/(.*).html /mypath/.php [L]
# http://www.example.com/CreateAccount/ to redirect to http://www.example.com/CreateAccount.php
RewriteRule ^(.*)/ /.php [L]
</IfModule>
例如,我宁愿 CreateAccount.php
在客户看来显示为 http://www.example.com/CreateAccount/
。我知道创建很多目录并用 index.php
填充每个目录很容易,但我宁愿做最有效的事情。首先,使用该方法需要高度无组织且不必要地复杂的组织标准。其次,我也宁愿充分利用 Apache 的特性,即 mod_rewrite
.
我知道有 mod_rewrite
。我开始阅读该模块的文档,但我很快变得 非常 困惑和有点沮丧,因为我有注意力缺陷多动症,所以当我知道自己没有注意力时很难坐下来阅读看不懂我在读什么。
你能帮我找到最好的方法吗?
接近 mod_rewrite 的最佳方法是创建一个测试 server/virtual 主机并慢慢构建您的配置。另外,尽量让你的初始规则非常简单,这样你就可以准确地理解每条规则的作用。此配置可以帮助您开始您的流程。最后一条规则是您需要将 CreateAccount/ 路径映射到 CreateAccount.php 脚本。
<IfModule mod_rewrite.c>
RewriteEngine On
# You can use this only inside of the Directory directive or in the .htaccess file.
RewriteBase /
# http://www.example.com/page.html to redirect to http://www.example.com/page.php
RewriteRule ^(.*).html /.php [L]
# http://www.example.com/mypath/page.html to redirect to http://www.example.com/mypath/page.php
RewriteRule ^mypath/(.*).html /mypath/.php [L]
# http://www.example.com/CreateAccount/ to redirect to http://www.example.com/CreateAccount.php
RewriteRule ^(.*)/ /.php [L]
</IfModule>