我的任务如下:我需要删除主页 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 放在根目录和你需要的根目录中
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
关于.htaccess - 如何使用 htaccess 中的重定向从 URL 中删除 index.php(主页)和 php(其他页面),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57338294/