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

标签 apache .htaccess redirect mod-rewrite https

我有两个 .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

我在公共(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 没有重定向到任何 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 路径pattern 并使用它代替 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 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/$1 [L]

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

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

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

关于apache - 使用两个 htaccess 文件将 www 重写为非 www 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66255400/

相关文章:

php - 该页面未使用 isset 正确重定向

c++ - 模仿unix shell管道

python - Errno 13 权限被拒绝的 bash 脚本和 apache2

apache - 在 Apache Web Server 前配置 NGINX Reverse Proxy

wordpress - 用于 Wordpress 和独立文件夹的 HTTPS 重定向

javascript - 如何使用 apache 和 html 捕获所有路由

wordpress - 在子目录(不是根目录)中运行 Wordpress

regex - 迭代 apache 2 日志文件名并使用 linux bash 比较数字

php - PHP 中的长时间计算导致 503 错误

tomcat - 将一个 tomcat 应用程序的不安全请求重定向到安全端口,并且不重定向另一个应用程序的请求