繁体   English   中英

使用 two.htaccess 文件将 www 重写为非 www 不起作用

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

我有两个.htaccess文件。

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

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ /public/index.php/home/index [L]
RewriteRule ^(.*)/$ $1
RewriteRule ^(.*)$ /public/$1 [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 ^(.*)/$ /$1 [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/$1 [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

我在公共文件夹.htaccess文件中添加了将www重定向到无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 重定向,否则您应该删除第一个检查HTTPS服务器变量的RewriteCond指令。 我还认为您应该在此处重定向到https:// ,而不是http://

你需要...

捕获RewriteRule模式中的 URL 路径并使用它代替REQUEST_URI服务器变量(这实际上是您在前面的规则中所做的)。 例如:

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

捕获的 URL 路径(即$1 )是相对于当前目录的,因此不包括/public子目录。 这也不包括斜杠前缀。

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

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

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/$1 [L]

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM