简体   繁体   English

htaccess 从 url 中删除 index.php

[英]htaccess remove index.php from url

I have a problem whereby google has indexed some pages with the wrong url.我有一个问题,即 google 用错误的 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. .htaccess 不是我的强项,所以任何帮助将不胜感激。

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/rewrite/intro.html(15分钟)并在阅读时参考这 2 页。

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/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.这是从 URL 中隐藏index.php的基本规则。 Put this in your root .htaccess file.把它放在你的根.htaccess文件中。

mod_rewrite must be enabled with PHP and this will work for the PHP version higher than 5.2.6.必须使用 PHP 启用mod_rewrite ,这适用于高于 5.2.6 的 PHP 版本。

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

Think %{REQUEST_FILENAME} as the the path after host.%{REQUEST_FILENAME}视为主机后的路径。

Eg https://www.example.com/index.html , %{REQUEST_FILENAME} is /index.html例如https://www.example.com/index.html%{REQUEST_FILENAME}/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 .所以最后 3 行意味着,如果它不是常规文件!-f而不是目录!-d ,那么执行RewriteRule

As for RewriteRule formats:至于 RewriteRule 格式:

在此处输入图片说明

So RewriteRule (.*) /index.php/$1 [L] means, if the 2 RewriteCond are satisfied, it (.*) would match everything after the hostname.所以RewriteRule (.*) /index.php/$1 [L]意味着,如果满足 2 RewriteCond ,它(.*)将匹配主机名之后的所有内容。 . matches any single character , .* matches any characters and (.*) makes this a variables can be references with $1 , then replace with /index.php/$1 .匹配任何单个字符, .*匹配任何字符并且(.*)使这个变量可以被$1引用,然后替换为/index.php/$1 The final effect is to add a preceding index.php to the whole URL path.最终的效果是在整个 URL 路径中添加一个前面的index.php

Eg for https://www.example.com/hello , it would produce, https://www.example.com/index.php/hello internally .例如,对于https://www.example.com/hello ,它会在内部生成https://www.example.com/index.php/hello

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.在内部,(我猜)它总是需要https://www.example.com/index.php/hello ,但是通过重写,您可以在没有index.php的情况下访问该站点,apache 在内部为您添加。


Btw, making an extra .htaccess file is not very recommended by the Apache doc.顺便说一句,Apache 文档不太推荐制作额外的.htaccess文件。

Rewriting is typically configured in the main server configuration setting (outside any <Directory> section) or inside <VirtualHost> containers.重写通常在主服务器配置设置中配置(在任何<Directory>部分之外)或在<VirtualHost>容器内。 This is the easiest way to do rewriting and is recommended这是最简单的重写方法,推荐使用

To remove index.php from the URL, and to redirect the visitor to the non-index.php version of the page:从 URL 中删除index.php ,并将访问者重定向到页面的非 index.php 版本:

RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

This will cleanly redirect /index.php/myblog to simply /myblog .这将干净地将/index.php/myblog重定向到简单的/myblog

Using a 301 redirect will preserve Google search engine rankings.使用 301 重定向将保留 Google 搜索引擎排名。

RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]

Assuming the existent url is假设现有的网址是

http://example.com/index.php/foo/bar

and we want to convert it into我们想把它转换成

 http://example.com/foo/bar

You can use the following rule :您可以使用以下规则:

RewriteEngine on
#1) redirect the client from "/index.php/foo/bar" to "/foo/bar"
RewriteCond %{THE_REQUEST} /index\.php/(.+)\sHTTP [NC]
RewriteRule ^ /%1 [NE,L,R]
#2)internally map "/foo/bar" to "/index.php/foo/bar"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php/$1 [L]

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.在 spep #1 中,我们首先匹配请求字符串并捕获/index.php/之后的所有内容,并将捕获的值保存在%1 var 中。 We then send the browser to a new url.然后我们将浏览器发送到一个新的 url。 The #2 processes the request internally. #2 在内部处理请求。 When the browser arrives at /foo/bar , #2rule rewrites the new url to the orignal location.当浏览器到达/foo/bar 时,#2rule 将新的 url 重写为原始位置。

Steps to remove index.php from url for your wordpress website.从 url 为您的 wordpress 网站删除 index.php 的步骤。

  1. Check you should have mod_rewrite enabled at your server.检查您是否应该在您的服务器上启用 mod_rewrite。 To check whether it's enabled or not - Create 1 file phpinfo.php at your root folder with below command.要检查它是否已启用 - 使用以下命令在您的根文件夹中创建 1 个文件 phpinfo.php。
 <?php
   phpinfo?();
 ?>

Now run this file - www.yoursite.com/phpinfo.php and it will show mod_rewrite at Load modules section.现在运行这个文件 - www.yoursite.com/phpinfo.php 它将在加载模块部分显示 mod_rewrite。 If not enabled then perform below commands at your terminal.如果未启用,则在您的终端执行以下命令。

sudo a2enmod rewrite
sudo service apache2 restart
  1. Make sure your .htaccess is existing in your WordPress root folder, if not create one .htaccess file Paste this code at your .htaccess file :-确保您的 .htaccess 存在于您的 WordPress 根文件夹中,如果没有创建一个 .htaccess 文件将此代码粘贴到您的 .htaccess 文件中:-
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.php$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.php [L]
</IfModule>  
  1. Further make permission of .htaccess to 666 so that it become writable and now you can do changes in your wordpress permalinks.进一步授权 .htaccess 到 666,使其可写,现在您可以在 wordpress 永久链接中进行更改。

  2. Now go to Settings -> permalinks -> and change to your needed url format.现在转到设置 -> 永久链接 -> 并更改为您需要的 url 格式。 Remove this code /index.php/%year%/%monthnum%/%day%/%postname%/ and insert this code on Custom Structure: /%postname%/删除此代码 /index.php/%year%/%monthnum%/%day%/%postname%/ 并在自定义结构中插入此代码:/%postname%/

  3. If still not succeeded then check your hosting, mine was digitalocean server, so I cleared it myself如果仍然没有成功,请检查您的主机,我的是digitalocean服务器,所以我自己清除了

Edited the file /etc/apache2/sites-enabled/000-default.conf编辑文件 /etc/apache2/sites-enabled/000-default.conf

Added this line after DocumentRoot /var/www/html在 DocumentRoot /var/www/html 之后添加了这一行

<Directory /var/www/html>
   AllowOverride All
</Directory>

Restart your apache server重启你的 apache 服务器

Note: /var/www/html will be your document root注意:/var/www/html 将是您的文档根目录

Do the following steps执行以下步骤

1. Make sure that the hosting / your pc mod_rewrite module is active. 1.确保主机/您的电脑 mod_rewrite 模块处于活动状态。 if not active then try to activate in a way, open the httpd.conf file.如果未激活,则尝试以某种方式激活,打开 httpd.conf 文件。 You can check this in the phpinfo.php to find out.您可以在 phpinfo.php 中检查以找出答案。

change this setting :更改此设置:

#LoadModule rewrite_module modules/mod_rewrite.so

to be and restart wamp成为并重新启动 wamp

LoadModule rewrite_module modules/mod_rewrite.so

2. Then go to .htaccess file, and try to modify to be: 2.然后进入.htaccess文件,尝试修改为:

RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]

if above does not work try with this:如果以上不起作用,请尝试以下操作:

RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php

3. Move .htaccess file to root directory, where is index.php there. 3.将.htaccess文件移动到根目录,index.php在那里。

www OR root folder
- index.php
- .htaccess

Some may get a 403 with the method listed above using mod_rewrite.有些人可能会使用 mod_rewrite 使用上面列出的方法获得 403。 Another solution to rewite index.php out is as follows:另一种重写 index.php 的解决方案如下:

<IfModule mod_rewrite.c> 

RewriteEngine On

# Put your installation directory here:
RewriteBase /

# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

</IfModule> 

I have used many codes from the above mentioned sections for removing index.php form the base url.我使用了上面提到的部分中的许多代码来从基本 url 中删除 index.php。 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.如果您确实需要从基本 URL 中删除 index.php,那么只需将此代码放在您的 htaccess 中即可。

RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]

RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

This will work, use the following code in .htaccess file RewriteEngine On这将起作用,在 .htaccess 文件中使用以下代码 RewriteEngine On

# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]

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我有https://example.com/entitlements/index.php而我希望任何输入它的人在请求事件时出错,如果你输入https://example.com/entitlements/index你仍然会得到错误,因为有这个包含“ index ”一词,尽管index.php的内容仍会正常显示,但总会抛出错误

cletus post on " https://stackoverflow.com/a/1055655/12192635 " which solved it cletus 在“ https://stackoverflow.com/a/1055655/12192635 ”上发帖解决了这个问题

Edit your .htaccess file with the below to redirect people visiting https://example.com/entitlements/index.php to 404 page使用以下内容编辑您的 .htaccess 文件,将访问https://example.com/entitlements/index.php 的人重定向到 404 页面

RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]

to redirect people visiting https://example.com/entitlements/index to 404 page将访问https://example.com/entitlements/index 的人重定向到 404 页面

RewriteCond %{THE_REQUEST} \index[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]

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.尽管我们已经知道上面的代码适用于堆栈上已经存在的代码,请参阅我将上面的代码应用到它末尾所有代码下方的位置。

# The following will allow you to use URLs such as the following:
#
#   example.com/anything
#   example.com/anything/
#
# Which will actually serve files such as the following:
#
#   example.com/anything.html
#   example.com/anything.php
#
# But *only if they exist*, otherwise it will report the usual 404 error.

Options +FollowSymLinks
RewriteEngine On

# Remove trailing slashes.
# e.g. example.com/foo/ will redirect to example.com/foo
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]

# Redirect to HTML if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

# Redirect to PHP if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]

RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]

RewriteCond %{THE_REQUEST} \index[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]

try this, it work for me试试这个,它对我有用

 <IfModule mod_rewrite.c> # Enable Rewrite Engine # ------------------------------ RewriteEngine On RewriteBase / # Redirect index.php Requests # ------------------------------ RewriteCond %{THE_REQUEST} ^GET.*index\\.php [NC] RewriteCond %{THE_REQUEST} !/system/.* RewriteRule (.*?)index\\.php/*(.*) /$1$2 [R=301,L] # Standard ExpressionEngine Rewrite # ------------------------------ RewriteCond $1 !\\.(css|js|gif|jpe?g|png) [NC] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php/$1 [L] </IfModule>

For more detail 欲知更多详情

create .htaccess file on project root directory and put below code for remove index.php在项目根目录上创建 .htaccess 文件并在下面的代码中删除 index.php

  RewriteEngine on
  RewriteCond $1 !^(index.php|resources|robots.txt)
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php/$1 [L,QSA]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM