htaccess 从 url 中删除 index.php
我遇到一个问题,谷歌用错误的网址索引了一些页面。
他们索引的网址是:
http://www.example.com/index.php/section1/section2
我需要它重定向到:
http://www.example.com/section1/section2
.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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
原来的答案实际上是正确的,但缺乏解释。我想添加一些解释和修改。
我建议阅读这个简短的介绍 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
这是从 URL 中隐藏
index.php
的基本规则。将其放入根.htaccess
文件中。mod_rewrite
必须在 PHP 中启用,这适用于高于 5.2.6 的 PHP 版本。将
%{REQUEST_FILENAME}
视为主机之后的路径。例如
https://www.example.com/index.html
,%{REQUEST_FILENAME}
是/index.html
所以最后 3 行意味着,如果它不是常规文件
!-f
也不是目录!-d
,则执行RewriteRule
。至于 RewriteRule 格式:
所以
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
,但是通过重写,您可以访问该网站而无需 < code>index.php,apache 在内部为您添加它。顺便说一句,Apache 文档并不非常建议创建额外的 .htaccess 文件。
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.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 theRewriteRule
.As for RewriteRule formats:
So
RewriteRule (.*) /index.php/$1 [L]
means, if the 2RewriteCond
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 precedingindex.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 withoutindex.php
, apache adds that for you internally.Btw, making an extra
.htaccess
file is not very recommended by the Apache doc.从 URL 中删除
index.php
,并将访问者重定向到页面的非 index.php 版本:这将干净地重定向
/index.php/myblog
简单地/myblog
。使用 301 重定向将保留 Google 搜索引擎排名。
To remove
index.php
from the URL, and to redirect the visitor to the non-index.php version of the page:This will cleanly redirect
/index.php/myblog
to simply/myblog
.Using a 301 redirect will preserve Google search engine rankings.
假设现有的 url 是
,我们想将其转换为
您可以使用以下规则:
在 spep #1 中,我们首先匹配请求字符串并捕获 /index.php/ 和捕获的值保存在 %1 var 中。然后我们将浏览器发送到一个新的 url。
#2 在内部处理该请求。当浏览器到达 /foo/bar 时,#2rule 将新的 url 重写到原始位置。
Assuming the existent url is
and we want to convert it into
You can use the following rule :
In the spep #1 we first match against the request string and capture everything after the /index.php/ and the captured value is saved in %1 var. We then send the browser to a new url.
The #2 processes the request internally. When the browser arrives at /foo/bar , #2rule rewrites the new url to the orignal location.
从 WordPress 网站的 url 中删除 index.php 的步骤。
要检查它是否已启用 - 使用以下命令在根文件夹中创建 1 个文件 phpinfo.php。
现在运行这个文件 - www.yoursite.com/phpinfo.php ,它将在加载模块部分显示 mod_rewrite 。
如果未启用,则在终端上执行以下命令。
将此代码粘贴到您的 .htaccess 文件中:-
进一步将 .htaccess 的权限设置为 666,以便它变得可写,现在您可以在 WordPress 永久链接中进行更改。
现在转到设置 ->永久链接 ->并更改为您需要的 url 格式。
删除此代码 /index.php/%year%/%monthnum%/%day%/%postname%/
并在自定义结构中插入此代码: /%postname%/
如果仍然不成功,请检查您的托管,我的是 digitalocean 服务器,所以我自己清除了它
编辑了文件 /etc/apache2/sites-enabled/000-default.conf
在 DocumentRoot /var/www/html 之后添加了这一行
重新启动您的 apache 服务器
注意:/var/www/html 将是你的文档根目录
Steps to remove index.php from url for your wordpress website.
To check whether it's enabled or not - Create 1 file phpinfo.php at your root folder with below command.
Now run this file - www.yoursite.com/phpinfo.php and it will show mod_rewrite at Load modules section.
If not enabled then perform below commands at your terminal.
Paste this code at your .htaccess file :-
Further make permission of .htaccess to 666 so that it become writable and now you can do changes in your wordpress permalinks.
Now go to Settings -> permalinks -> and change to your needed url format.
Remove this code /index.php/%year%/%monthnum%/%day%/%postname%/
and insert this code on Custom Structure: /%postname%/
If still not succeeded then check your hosting, mine was digitalocean server, so I cleared it myself
Edited the file /etc/apache2/sites-enabled/000-default.conf
Added this line after DocumentRoot /var/www/html
Restart your apache server
Note: /var/www/html will be your document root
执行以下步骤
1. 确保主机/您的电脑 mod_rewrite 模块处于活动状态。如果未激活,则尝试以某种方式激活,打开 httpd.conf 文件。您可以在 phpinfo.php 中检查这一点以找出答案。
更改此设置:
并重新启动 wamp
2. 然后转到 .htaccess 文件,并尝试修改为:
如果上述不起作用,请尝试使用此:
3. 将.htaccess文件移动到根目录,其中有index.php。
Do the following steps
1. Make sure that the hosting / your pc mod_rewrite module is active. if not active then try to activate in a way, open the httpd.conf file. You can check this in the phpinfo.php to find out.
change this setting :
to be and restart wamp
2. Then go to .htaccess file, and try to modify to be:
if above does not work try with this:
3. Move .htaccess file to root directory, where is index.php there.
有些人可能会使用 mod_rewrite 使用上面列出的方法得到 403。另一种重写index.php的解决方案如下:
Some may get a 403 with the method listed above using mod_rewrite. Another solution to rewite index.php out is as follows:
我使用了上述部分中的许多代码来从基本网址中删除index.php。但从我的角度来看,它不起作用。因此,您可以使用我已经使用过的代码并且它可以正常工作。
如果您确实需要从基本 URL 中删除 index.php,那么只需将此代码放入您的 htaccess 中即可。
I have used many codes from the above mentioned sections for removing index.php form the base url. But it was not working from my end. So, you can use this code which I have used and its working properly.
If you really need to remove index.php from the base URL then just put this code in your htaccess.
这将起作用,在 .htaccess 文件中使用以下代码
重写引擎开启
This will work, use the following code in .htaccess file
RewriteEngine On
我不需要很多庞大的代码来给出一个小片段就可以解决我的问题。
我有https://example.com/entitlements/index.php 相反,我希望任何键入它的人在输入 https://example.com/entitlements/index
你仍然会得到错误,因为包含这个词“index”,尽管index.php的内容仍然会正确显示,但总会有一个错误被抛出
使用以下内容编辑您的 .htaccess 文件
将访问 https://example.com/entitlements/index.php 的用户重定向到 404 页面
将访问 https://example.com/entitlements/index 的人重定向到 404 页面
尽管我们有已经知道上面的代码可以与堆栈上已有的代码一起使用,请参阅我在其末尾的所有代码下方应用上面的代码的位置。
I don't have to many bulky code to give out just a little snippet solved the issue for me.
i have https://example.com/entitlements/index.php rather i want anyone that types it to get error on request event if you type https://example.com/entitlements/index
you will still get error since there's this word "index" is contained there will always be an error thrown back though the content of index.php will still be displayed properly
Edit your .htaccess file with the below
to redirect people visiting https://example.com/entitlements/index.php to 404 page
to redirect people visiting https://example.com/entitlements/index to 404 page
Not withstanding we have already known that the above code works with already existing codes on stack see where i applied the code above just below the all codes at it end.
试试这个,它对我有用
try this, it work for me
THE_REQUEST
与大多数答案一样,应被视为最后的手段。最好使用REQUEST_URI
。以下代码执行 301 重定向,并适用于
/index.php
和/index.php/foo/bar
。THE_REQUEST
as in most of the answers should be considered a last resort. Better useREQUEST_URI
.The following performs a 301 redirect and works with both
/index.php
and/index.php/foo/bar
.