使用两个 htaccess 文件将 www 重写为非 www 不起作用

rewrite www to non www doesn't work using two htaccess file

我有两个 .htaccess 文件。

在根目录中:(此文件重定向到 public 文件夹)

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ /public/index.php/home/index [L]
RewriteRule ^(.*)/$ 
RewriteRule ^(.*)$ /public/ [L]
</IfModule>

public 文件夹中我有:

# Disable directory browsing
Options All -Indexes

# ----------------------------------------------------------------------
# Rewrite engine
# ----------------------------------------------------------------------

# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On

    # If you installed CodeIgniter in a subfolder, you will need to
    # change the following line to match the subfolder you need.
    # http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
    # RewriteBase /

    # Redirect Trailing Slashes...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ / [L,R=301]

    # Rewrite "www.example.com -> example.com"
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,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 the front controller, index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([\s\S]*)$ index.php/ [L,NC,QSA]

    # Ensure Authorization header is passed along
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    ErrorDocument 404 index.php
</IfModule>
# X-XSS-Protection
<IfModule mod_headers.c>
    Header set X-XSS-Protection "1; mode=block"
</IfModule>
# X-Frame-Options
<IfModule mod_headers.c>
    Header always append X-Frame-Options SAMEORIGIN
</IfModule>
# X-Content-Type nosniff
<IfModule mod_headers.c>
    Header set X-Content-Type-Options nosniff
</IfModule>
# Disable server signature start
    ServerSignature Off
# Disable server signature end

我在 public 文件夹 .htaccess 文件中添加此代码以将 www 重定向到 none www

# Rewrite "www.example.com -> example.com"
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

但实际上,我的 www 没有重定向到 none www。我该如何解决这个问题?

# Rewrite "www.example.com -> example.com"
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

这里的问题是 REQUEST_URI 服务器变量包含完整的 URL 路径,包括 /public 子目录,因此这将暴露 /public 子目录重定向。

旁白: 另外,如评论中所述,除非您只想对 HTTP 请求执行 www 到非 www 重定向,否则您应该删除第一个 RewriteCond 指令检查 HTTPS 服务器变量。我还认为您应该在此处重定向到 https://,而不是 http://?

您需要...

捕获 RewriteRule 模式 中的 URL-path 并使用它代替 REQUEST_URI 服务器变量(这实际上是什么你正在做前面的规则)。例如:

# Rewrite "www.example.com -> example.com"
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule (.*) https://%1/ [R=301,L]

捕获的 URL-路径(即 </code>)是相对于当前目录的,因此不包括 <code>/public 子目录。这也排除了斜杠前缀。

但是,如果没有额外的指令,上面的内容将公开 /index.php/home/index URL(在向根请求的情况下)。

或者,将此重定向移动到根 .htaccess 文件,这是更可取的,因为它自然会防止 /index.php/home/index URL 被暴露。例如:

RewriteEngine On

# Redirect "www.example.com -> example.com"
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

# Rewrite to /public subdirectory
RewriteRule ^$ public/index.php/home/index [L]
RewriteRule ^(.+?)/?$ public/ [L]

(不需要 <IfModule> 包装器。)

请注意,您应该首先使用 302(临时)重定向进行测试,以避免潜在的缓存问题。只有当您确定它按预期工作时才更改为 301(永久)。

测试前您需要清除浏览器缓存。