htaccess删除索引。php从url

时间:2021-09-08 11:19:51

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

我有个问题,谷歌用错误的url索引了一些页面。

The url they are indexing is:

他们索引的url是:

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.

提前谢谢。

6 个解决方案

#1


45  

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

这是隐藏索引的基本规则。php从URL。把这个放到根.htaccess文件中。

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

mod_rewrite必须使用PHP,这对于5.2.6版本的PHP来说是可行的。

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

#2


30  

To remove index.php from the URL, and to redirect the visitor to the non-index.php version of the page:

删除索引。来自URL的php,并将访问者重定向到非索引。页面的php版本:

RewriteBase /
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.

这将清晰地重定向/索引。php / myblog简单/ myblog。

Using a 301 redirect will preserve Google search engine rankings.

使用301重定向将保留谷歌搜索引擎排名。

#3


16  

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

#4


4  

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.

1。确保主机/您的pc mod_rewrite模块是活动的。如果没有激活,那么尝试以某种方式激活httpd。conf文件。你可以在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文件移动到根目录,其中是索引。php。

www OR root folder
- index.php
- .htaccess

#5


4  

Assuming the existent url is

假设存在的url是。

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. 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.

在spep #1中,我们首先匹配请求字符串并捕获/索引之后的所有内容。将捕获的值保存在%1 var中,然后将浏览器发送到一个新的url。#2在内部处理请求。当浏览器到达/foo/bar时,#2规则将新url重写到orignal位置。

#6


2  

Steps to remove index.php from url for your wordpress website.

步骤删除索引。php从url为你的wordpress网站。

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

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.

现在运行这个文件——www.yoursite.com/phpinfo.php,它将在加载模块部分显示mod_rewrite。如果未启用,则在终端执行以下命令。

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 :-
  2. 确保你的。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 permalinks中进行更改。

  2. 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%/

    现在转到Settings -> permalinks ->并更改为所需的url格式。删除这段代码/索引。php/%year%/%month %/%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将是您的文档根

#1


45  

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

这是隐藏索引的基本规则。php从URL。把这个放到根.htaccess文件中。

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

mod_rewrite必须使用PHP,这对于5.2.6版本的PHP来说是可行的。

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

#2


30  

To remove index.php from the URL, and to redirect the visitor to the non-index.php version of the page:

删除索引。来自URL的php,并将访问者重定向到非索引。页面的php版本:

RewriteBase /
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.

这将清晰地重定向/索引。php / myblog简单/ myblog。

Using a 301 redirect will preserve Google search engine rankings.

使用301重定向将保留谷歌搜索引擎排名。

#3


16  

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

#4


4  

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.

1。确保主机/您的pc mod_rewrite模块是活动的。如果没有激活,那么尝试以某种方式激活httpd。conf文件。你可以在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文件移动到根目录,其中是索引。php。

www OR root folder
- index.php
- .htaccess

#5


4  

Assuming the existent url is

假设存在的url是。

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. 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.

在spep #1中,我们首先匹配请求字符串并捕获/索引之后的所有内容。将捕获的值保存在%1 var中,然后将浏览器发送到一个新的url。#2在内部处理请求。当浏览器到达/foo/bar时,#2规则将新url重写到orignal位置。

#6


2  

Steps to remove index.php from url for your wordpress website.

步骤删除索引。php从url为你的wordpress网站。

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

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.

现在运行这个文件——www.yoursite.com/phpinfo.php,它将在加载模块部分显示mod_rewrite。如果未启用,则在终端执行以下命令。

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 :-
  2. 确保你的。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 permalinks中进行更改。

  2. 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%/

    现在转到Settings -> permalinks ->并更改为所需的url格式。删除这段代码/索引。php/%year%/%month %/%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将是您的文档根