gpt4 book ai didi

apache - htaccess 从 url 中删除 index.php

转载 作者:行者123 更新时间:2023-12-03 06:55:46 25 4
gpt4 key购买 nike

我遇到了一个问题,即 Google 使用错误的网址对某些页面建立了索引。

他们索引的网址是:

http://www.example.com/index.php/section1/section2

我需要它重定向到:

http://www.example.com/section1/section2

.htaccess 不是我的强项,因此我们将不胜感激。

最佳答案

原来的答案实际上是正确的,但缺乏解释。我想添加一些解释和修改。

我建议阅读这篇简短的介绍 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/rewrite/flags.html

<小时/>

这是隐藏的基本规则index.php从网址。将其放入您的根目录 .htaccess文件。

mod_rewrite必须使用 PHP 启用,这适用于高于 5.2.6 的 PHP 版本。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php/$1 [L]
<小时/>

思考%{REQUEST_FILENAME}作为主机之后的路径。

例如https://www.example.com/index.html , %{REQUEST_FILENAME}/index.html

所以最后 3 行意味着,如果它不是一个常规文件 !-f而不是目录 !-d ,然后执行 RewriteRule .

至于RewriteRule格式:

enter image description here

所以RewriteRule (.*) /index.php/$1 [L]意思是,如果 2 RewriteCond满意吧(.*)将匹配主机名之后的所有内容。 .匹配任何单个字符,.*匹配任何字符(.*)使得这个变量可以用 $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 文档不太推荐该文件。

Rewriting is typically configured in the main server configurationsetting (outside any <Directory> section) or inside <VirtualHost>containers. This is the easiest way to do rewriting and is recommended

关于apache - htaccess 从 url 中删除 index.php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4365129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com