原来的答案实际上是正确的,但缺乏解释。我想添加一些解释和修改。
我建议阅读这个简短的介绍https://httpd.apache.org/docs/2.4/rewrite/intro.html https://httpd.apache.org/docs/2.4/rewrite/intro.html(15 分钟)并在阅读时参考这两页。
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
https://httpd.apache.org/docs/2.4/rewrite/flags.html https://httpd.apache.org/docs/2.4/rewrite/flags.html
这是隐藏的基本规则index.php
从网址。把这个放在你的根目录下.htaccess
file.
mod_rewrite
必须使用 PHP 启用,这适用于高于 5.2.6 的 PHP 版本。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php/$1 [L]
Think %{REQUEST_FILENAME}
作为主机之后的路径。
E.g. https://www.example.com/index.html
, %{REQUEST_FILENAME}
is /index.html
所以最后 3 行意味着,如果它不是一个常规文件!-f
而不是一个目录!-d
,然后执行RewriteRule
.
至于 RewriteRule 格式:
So RewriteRule (.*) /index.php/$1 [L]
意味着,如果 2RewriteCond
很满意,它(.*)
将匹配主机名之后的所有内容。.
匹配任何单个字符,.*
匹配任何人物 and (.*)
使得这个变量可以被引用$1
,然后替换为/index.php/$1
。最后的效果就是在前面加上一个index.php
到整个 URL 路径。
例如。为了https://www.example.com/hello
,它会产生,https://www.example.com/index.php/hello
内部.
另一个关键问题是,这确实解决了问题。内部,(我猜)它总是需要https://www.example.com/index.php/hello
,但是通过重写,您可以访问该网站而无需index.php
,apache 在内部为您添加了该内容。
顺便说一句,额外做.htaccess
Apache 文档不太推荐该文件。
重写通常在主服务器配置中配置
设置(任何外部<Directory>
部分)或内部<VirtualHost>
容器。这是最简单的重写方法,推荐使用