【问题标题】:How to remove both index.php (homepage) and php (other pages) from URLs with redirects in htaccess如何在 htaccess 中使用重定向从 URL 中删除 index.php(主页)和 php(其他页面)
【发布时间】:2019-12-11 18:41:47
【问题描述】:

我的任务如下:我需要删除主页URL中的index.php和所有页面的url中的php。 (目前我已经为所有页面设置了规范标签以避免重复)。

已尝试的解决方案:我在此处查看了有关如何在 url 中删除 index.php 和 php 的所有答案。

问题:但是,当我在 htaccess 中同时使用这两个解决方案时,只有其中一个可以工作。例如,使用上面的代码,我无法删除 url 中的 php。此外,它会为某些页面生成 404 错误。你可以在这里看到:https://efficaceweb.fr/referencement-naturel-optimisation-site-seo

然后创建了奇怪的 URL:/index.php/audit-site-seo-referencement-naturel.php

任何有 htaccess 编码经验的人可以告诉我我的代码有什么问题吗?我将非常感激。提前致谢。

<IfModule mod_rewrite.c>

#redirect from www

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]

#remove php in url

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

#remove index php in url

RewriteRule ^(.*)index\.php$ /$1 [R=301,NC]

#https redirect

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

</IfModule>

我希望有来自index.php 的主页重定向和来自.php 的所有其他页面的重定向,并且不会生成错误和奇怪的url。谢谢

【问题讨论】:

  • 这是我在 htaccess 中的完整代码: #redirect from www RewriteEngine on RewriteCond %{HTTP_HOST} ^www\.(.*)$ RewriteRule ^(.*)$ https: //%1/$1 [L,R=301] #remove index php in url RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L] #https 重定向 RewriteCond %{HTTPS} !=on RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

标签: .htaccess url redirect https seo


【解决方案1】:

要删除 index.php,您需要在 href 中添加它

<a href="/"></a>

而不是

<a href="/index.php"></a>

要从 url 中删除 .php,您需要将 .htaccess 放在根目录和根目录中

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

然后从链接href中删除.php,例如 &lt;a href="post"&gt;&lt;/a&gt;删除.php

【讨论】:

  • 感谢 Bilawal 的帖子。但是,我没有 。我确实有 。而且我的代码中没有 .php 。您可以签入源代码。顺便说一句,它不能解决问题,因为它只是一个链接。在我的菜单和所有导航中,我都有短 URL,没有 .php。我的问题是将 301 从 .php 重定向到非 .php。谢谢
  • 我已经试过你的代码和这个代码 RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L] ...但没有一个作品
猜你喜欢
  • 2013-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
  • 1970-01-01
  • 2018-02-09
  • 2022-01-19
  • 1970-01-01
相关资源
最近更新 更多