.htaccess:从URL中删除index.php,强制使用非www和https


.htaccess: removing index.php from URL, force non-www and https

我在.htaccess文件中有以下代码:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www'.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

一般来说,URL中没有index.php。但是,当我尝试在URL前面添加www时。它提供了index.php文件。

例如

https://example.com/settings/update_rates

在URL前面添加www后:

https://example.com/index.php?/settings/update_rates

如何在此处删除index.php

使用this.htaccess

### Canonicalize codeigniter URLs
# Enforce SSL https://www. 
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
###
# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

堆栈应答

以下是Codeigniter理想的.htaccess文件。

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    ### Canonicalize codeigniter URLs
    # If your default controller is something other than
    # "welcome" you should probably change this
    RewriteRule ^(welcome(/index)?|index('.php)?)/?$ / [L,R=301]
    RewriteRule ^(.*)/index/?$ $1 [L,R=301]
    # Removes trailing slashes (prevents SEO duplicate content issues)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/$ $1 [L,R=301]
    # Enforce www
    # If you have subdomains, you can add them to 
    # the list using the "|" (OR) regex operator
    RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
    RewriteRule ^(.*)$ http://www.domain.tld/$1 [L,R=301]
    # Enforce NO www
    #RewriteCond %{HTTP_HOST} ^www [NC]
    #RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301]
    ###
    # Removes access to the system folder by users.
    # Additionally this will allow you to create a System.php controller,
    # previously this would not have been possible.
    # 'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php/$1 [L]
    # Checks to see if the user is attempting to access a valid file,
    # such as an image or css document, if this isn't true it sends the
    # request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
    # Without mod_rewrite, route 404's to the front controller
    ErrorDocument 404 /index.php
</IfModule>

之后转到/system/application/config/config.php然后找到下面的

$config['index_page'] = "index.php";

按照

$config['index_page'] = "";

因为您使用的是Apache mod_rewrite,所以上面会设置您的配置文件,使"index.php"不会显示为URL 的一部分

您应该尝试一下。我喜欢把我的外部重定向放在第一位。您也可以使用您的域名来组合这些规则。除非您在多个域中使用通配符,否则使用通配符毫无意义。

RewriteEngine On
RewriteCond %{HTTPS} !^on [OR]
RewriteCond %{HTTP_HOST} ^www'. [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond $1 !^(index'.php|resources|robots'.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

然后在config.php文件中确保您已经删除了index.php.

查找以下代码:-

$config['index_page'] = 'index.php'; 

替换为以下代码:-

$config['index_page'] = '';