如何删除Drupal网站URL链接中的index.php字符

Submitted by phifans on Fri, 03/22/2024 - 10:06

Drupal网站更新后,发现网站URL链接会随机出现index.php,比如https://www.phifans.com/index.php/a/remove-index-php-from-drupal-url.html,你们链接中出现的index.php要如何删掉呢?

删除Drupal网站URL链接中的index.php

找到Drupal网站的.htaccess文件并打开。

添加如下代码并保存

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

运行cron,清空缓存,这样Drupal网站url链接中的index.php就不会再出现了。URL也变成正常的https://www.phifans.com/a/remove-index.php-from-drupal-url.html了。

以下代码也可以,增加一些规则。

# Remove trailing /index.php that comes from external links.
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]
# Pass all requests not referring directly to files in the filesystem to
# index.php.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]

 

栏目