【问题标题】:htaccess remove index.php and add www to url in expressionenginehtaccess 删除 index.php 并将 www 添加到表达式引擎中的 url
【发布时间】:2016-01-22 17:17:20
【问题描述】:

我通过表达式引擎创建了一个网站,在 htaccess 中我需要删除 index.php 并将 www 添加到无 www url。 这是我当前的 .htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Add www to url
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

    # Directs all EE web requests through the site index file
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

    # Removes index.php from ExpressionEngine URLs
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{REQUEST_URI} !/system/.* [NC]
    RewriteRule (.*?)/index\.php/*(.*) /$1$2 [R=301,NE,L]

</IfModule>

但是上面的代码有一些问题,当 url 有 www 本身并且没有 index.php 并且一切都很好,但是当我从 url 中删除 www 以测试“添加 www”部分时,它可以工作它不再起作用,并且网址损坏如下:

http://www.example.com/index.php?/segment1/segment2/

一个 index.php 出现了。我不知道该怎么做,任何帮助都会得到帮助

【问题讨论】:

    标签: php .htaccess codeigniter expressionengine


    【解决方案1】:

    试试这个..

    RewriteEngine On
    
    #Redirect non-www to www
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
    

    它将从 URL 中删除 index.php 并添加 www 作为前缀。

    【讨论】:

    • 实际上它是单独工作的,问题是当我想删除 index.php 并添加 www
    • @Ajay - 在尝试了许多规则之后,最后这段代码对我有用。
    • 随时为您提供帮助 :)
    【解决方案2】:

    www 重定向可能不起作用,因为没有通配符,保持简单并指定您的域:

    # Redirect to www
    # -------------------
    RewriteCond %{HTTP_HOST} ^example.com [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
    

    我希望您以错误的方式删除和添加 index.php,如果在 URL 中指定,我首先将其重定向,然后将其添加到隐藏中:

    # Redirect specific index.php requests in URL
    # ------------------------------
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{REQUEST_URI} !/system/.* [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]
    
    # Directs all EE web requests through the site index file
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
    

    还可以尝试使用和不使用 ?在这一行,根据服务器设置,可能不需要:

    RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
    

    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
    

    【讨论】:

      猜你喜欢
      • 2014-08-24
      • 2012-06-23
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      • 1970-01-01
      • 2021-04-08
      • 2017-09-14
      • 1970-01-01
      相关资源
      最近更新 更多