【问题标题】:How to remove index.php from URLs如何从 URL 中删除 index.php
【发布时间】:2016-09-18 21:37:04
【问题描述】:

我想知道如何从 URL 中删除 index.php,我网站上的每个链接都有 index.php,例如example.com/index.php/contact-us。

我使用 Drupal 8,我的服务器是 Apache,php 版本是 5.6,我在共享主机上。

在我尝试使用的 .htaccess 文件中

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

删除了 index.php,但任何新文章都不会在主页中显示图像(index.php)

然后我尝试使用

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

同样的事情发生了。

请指教

【问题讨论】:

    标签: apache drupal drupal-8


    【解决方案1】:

    这是我添加到我的 apache 配置中的内容。

    # Sometimes (multiple instances of) index.php/ get(s) inserted in URLs
    # Remove them
    RewriteCond %{REQUEST_URI} ^/(index.php/)+(.*)
    RewriteRule ^.*$ /%2 [R=301,END]
    

    显然,一些“简单的 XML 站点地图”版本引入了这些 index.php 部分并将它们提交给搜索引擎,现在我得到了那些不干净的 url。

    将 drupal 配置为不使用它们是一回事,但告诉客户这些 URL 不再有效是另一回事。这就是上面派上用场的地方。它只是删除 URL 中的所有 index.php/ 部分。

    注意:这必须放在所有其他重写规则之前!

    【讨论】:

    • 这终于对我有用了。在查看从简单站点地图生成的 sitemap.xml 时偶然发现了它。现在它又开始工作了!
    【解决方案2】:

    请查看以下 Drupal 8 文档:https://www.drupal.org/docs/8/configuring-clean-urls/enable-clean-urls

    这应该可以帮助您启用干净的 URL。

    另外:您可能需要检查以下问题和 cmets 以解决您的问题:Enable Clean URLs

    扩展:尝试检查图像上的 src URL(如果它们包含 index.php,否则可能是权限配置错误)

    编辑:在您的 .htaccess 中尝试此代码

      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_URI} !=/favicon.ico
      RewriteRule ^ index.php [L]
    

    附带说明:您可能需要考虑切换到 PHP7,因为对 PHP5.6 的积极支持即将结束。 (Source)

    【讨论】:

    • 我在那里尝试了几乎所有东西,仍然面临同样的问题。
    • 我也试过了,还是一样的问题,现在当我在 RewriteCond %{REQUEST_FILENAME} 上使用这个 RewriteEngine 时!-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index .php?/$1 [L] 它实际上添加了 index.php !!!有趣的是,我的本地主机上的同一个网站运行良好,每个 url 中没有任何 index.php(我使用相同的 .htaccess 文件)
    • 再次更新了我的答案:P
    • 这是我现在拥有的相同代码,我向上帝发誓我在这里发疯了,我不知道我到底做了什么,但是使用你现在发布的这段代码,这是我的默认设置,一切都是现在工作。但是当我访问我的 /user 页面时,它会将我重定向到 index.php 然后我需要访问 site.com/index.php/user
    猜你喜欢
    • 2012-05-15
    • 2013-05-15
    • 2019-08-16
    • 2012-08-10
    • 2015-10-24
    • 1970-01-01
    相关资源
    最近更新 更多