htaccess 从 url 中删除 index.php [英] htaccess remove index.php from url

查看:28
本文介绍了htaccess 从 url 中删除 index.php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,即 google 用错误的 url 索引了一些页面.

他们索引的网址是:

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

我需要它重定向到:

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

.htaccess 不是我的强项,所以任何帮助将不胜感激.

提前致谢.

解决方案

原来的答案其实是正确的,只是缺少解释.我想补充一些解释和修改.

我建议阅读这篇简短的介绍

所以 RewriteRule (.*)/index.php/$1 [L] 的意思是,如果 2 RewriteCond 满足,它 (.*) 将匹配主机名之后的所有内容.. 匹配任何单个字符,.* 匹配任何字符(.*) 使这成为一个变量可以使用 $1 引用,然后替换为 /index.php/$1.最后的效果是在整个URL路径中添加一个前面的index.php.

例如对于 https://www.example.com/hello,它会在内部生成 https://www.example.com/index.php/hello .

另一个关键问题是这确实解决了问题.内部,(我猜)它总是需要https://www.example.com/index.php/hello,但是通过重写,您可以在没有index.php,apache 在内部为你添加.


顺便说一句,Apache 文档不推荐制作额外的 .htaccess 文件.

<块引用>

重写一般配置在主服务器配置中设置(在任何 部分之外)或在 内容器.这是最简单的重写方法,推荐使用

I have a problem whereby google has indexed some pages with the wrong url.

The url they are indexing is:

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

I need it to redirect to:

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

.htaccess isn't my forte, so any help would be much appreciated.

Thanks in advance.

解决方案

The original answer is actually correct, but lacks explanation. I would like to add some explanations and modifications.

I suggest reading this short introduction https://httpd.apache.org/docs/2.4/rewrite/intro.html (15mins) and reference these 2 pages while reading.

https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html https://httpd.apache.org/docs/2.4/rewrite/flags.html


This is the basic rule to hide index.php from the URL. Put this in your root .htaccess file.

mod_rewrite must be enabled with PHP and this will work for the PHP version higher than 5.2.6.

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


Think %{REQUEST_FILENAME} as the the path after host.

E.g. https://www.example.com/index.html, %{REQUEST_FILENAME} is /index.html

So the last 3 lines means, if it's not a regular file !-f and not a directory !-d, then do the RewriteRule.

As for RewriteRule formats:

So RewriteRule (.*) /index.php/$1 [L] means, if the 2 RewriteCond are satisfied, it (.*) would match everything after the hostname. . matches any single character , .* matches any characters and (.*) makes this a variables can be references with $1, then replace with /index.php/$1. The final effect is to add a preceding index.php to the whole URL path.

E.g. for https://www.example.com/hello, it would produce, https://www.example.com/index.php/hello internally.

Another key problem is that this indeed solve the question. Internally, (I guess) it always need https://www.example.com/index.php/hello, but with rewriting, you could visit the site without index.php, apache adds that for you internally.


Btw, making an extra .htaccess file is not very recommended by the Apache doc.

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

这篇关于htaccess 从 url 中删除 index.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆