htaccess从url中删除index.php

Nic*_*ick 42 apache .htaccess mod-rewrite

我有一个问题,谷歌已经使用错误的网址索引了一些网页.

他们索引的网址是:

http://www.example.com/index.php/section1/section2
Run Code Online (Sandbox Code Playgroud)

我需要它重定向到:

http://www.example.com/section1/section2
Run Code Online (Sandbox Code Playgroud)

.htaccess不是我的强项,所以任何帮助都会非常感激.

提前致谢.

Sha*_*ngh 60

这是隐藏index.phpURL 的基本规则.把它放在你的根.htaccess文件中.

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

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

  • 这不会删除index.php,而是添加它! (23认同)
  • 问题有变吗?此解决方案是将index.php添加到没有(或确实)拥有它的URL. (8认同)
  • 如果您真的在为SEO工作,则无需将连字符转换为下划线.人们正在为SEO做反向.连字符是谷歌友好的. (7认同)
  • 请解释你的所作所为.99%的人不理解你在3行中做了什么. (7认同)
  • 这两个建议是完全一样的吗? (3认同)

Bra*_*ood 50

要从index.phpURL中删除,并将访问者重定向到页面的非index.php版本:

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

这将干净地重定向/index.php/myblog到简单/myblog.

使用301重定向将保留Google搜索引擎排名.


Ste*_*als 18

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


sta*_*een 10

假设存在的url是

http://example.com/index.php/foo/bar
Run Code Online (Sandbox Code Playgroud)

我们想把它转换成

 http://example.com/foo/bar
Run Code Online (Sandbox Code Playgroud)

您可以使用以下规则:

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]
Run Code Online (Sandbox Code Playgroud)

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


Kai*_*las 5

执行以下步骤

1.确保主机/您的PC mod_rewrite模块处于活动状态。如果未激活,请尝试以某种方式激活,打开httpd.conf文件。您可以在phpinfo.php中进行检查以找出答案。

更改此设置:

#LoadModule rewrite_module modules/mod_rewrite.so
Run Code Online (Sandbox Code Playgroud)

并重新启动沼泽

LoadModule rewrite_module modules/mod_rewrite.so
Run Code Online (Sandbox Code Playgroud)

2.然后转到.htaccess文件,尝试将其修改为:

RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
Run Code Online (Sandbox Code Playgroud)

如果上述方法不起作用,请尝试以下操作:

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
Run Code Online (Sandbox Code Playgroud)

3.将.htaccess文件移动到根目录,那里是index.php。

www OR root folder
- index.php
- .htaccess
Run Code Online (Sandbox Code Playgroud)


小智 5

从您的wordpress网站的网址中删除index.php的步骤。

  1. 检查您应该在服务器上启用mod_rewrite。要检查是否已启用-使用以下命令在您的根文件夹中创建1个文件phpinfo.php。
 <?php
   phpinfo?();
 ?>
Run Code Online (Sandbox Code Playgroud)

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

sudo a2enmod rewrite
sudo service apache2 restart
Run Code Online (Sandbox Code Playgroud)
  1. 如果没有创建一个.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>  
Run Code Online (Sandbox Code Playgroud)
  1. 进一步对666授予.htaccess权限,使其可写,现在您可以更改wordpress永久链接。

  2. 现在转到设置->永久链接->并更改为所需的URL格式。删除此代码/index.php/%year%/%monthnum%/%day%/%postname%/并将此代码插入自定义结构中:/%postname%/

  3. 如果仍然没有成功,请检查您的主机,我的服务器是Digitalocean服务器,所以我自己清除了它

编辑了文件/etc/apache2/sites-enabled/000-default.conf

在DocumentRoot / var / www / html之后添加了这一行

<Directory /var/www/html>
   AllowOverride All
</Directory>
Run Code Online (Sandbox Code Playgroud)

重新启动Apache服务器

注意:/ var / www / html将成为您的文档根目录