【发布时间】: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