防止其他www页面重定向到首页

问题描述 投票:0回答:1

所以我的主要目标是:

  • http -> https
  • www -> 非 www
  • 从网址中删除index.php

完成上述操作后,我遇到的问题是像https://www.disposable.network/api这样的页面重定向到https://disposable.network,而不是重定向到https://disposable。网络/API

我当前的.htaccess:

<IfModule mod_rewrite.c>
    #start: code from script
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
    #end: code from script

    # Redirect http to https
    RewriteCond %{HTTPS} !on
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA]

    # Redirect non-www to www
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule (.*) https://disposable\.network/$1 [R=301,L,QSA]

    #remove index.php from url
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
</IfModule>
apache .htaccess
1个回答
0
投票
# Redirect http to https
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA]

# Redirect non-www to www
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule (.*) https://disposable\.network/$1 [R=301,L,QSA]

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

您的规则顺序错误。您的规范重定向需要在“前控制器模式”之前(重写为index.php

)。

由于您在第二条规则中对主机名进行硬编码,因此该规则上的第二个

条件是多余的。

© www.soinside.com 2019 - 2024. All rights reserved.