如何从HTACCESS中重定向的URL中删除index.php(主页)和PHP(其他页面)



我的任务是:我需要在主页的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的页面。谢谢

用于删除index.php,您需要在href

中添加它
<a href="/"></a>

而不是

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

并且要从URL中删除.php,您需要将.htaccess放在root目录中,并且内部需要

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

,然后从链接HREF中删除.php <a href="post"></a>删除.php

最新更新