Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess 如何使用查询参数将所有www请求重定向到非www_.htaccess_Mod Rewrite_Url Rewriting_Apache2 - Fatal编程技术网

.htaccess 如何使用查询参数将所有www请求重定向到非www

.htaccess 如何使用查询参数将所有www请求重定向到非www,.htaccess,mod-rewrite,url-rewriting,apache2,.htaccess,Mod Rewrite,Url Rewriting,Apache2,我正在尝试将所有www URL重定向到非www URL 发件人: 1. http://example.com/web-url 2. http://www.example.com/web-url 3. https://www.example.com/web-url 致: 我使用的配置如下 RewriteEngine on # If a directory or a file exists, use it directly RewriteCond %{REQUEST_FILENAME} !-f

我正在尝试将所有www URL重定向到非www URL

发件人:

1. http://example.com/web-url
2. http://www.example.com/web-url
3. https://www.example.com/web-url
致:

我使用的配置如下

RewriteEngine on

# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Otherwise forward it to index.php
RewriteRule . index.php

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
综上所述,一切似乎都没问题,但第三个选项没有按预期工作。问题是
https://www.example.com/web-url
被重定向到
https://example.com/index.php


有人能告诉我我的配置有什么问题吗?

更改规则的顺序按预期工作

RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

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

# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Otherwise forward it to index.php
RewriteRule . index.php

在进行内部重写之前,您需要执行外部重定向。@CBroe我该怎么做?通过更改.htaccess文件中这些行的顺序…@CBroe,谢谢,伙计,成功了。更改上述规则的顺序按预期工作。
RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

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

# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Otherwise forward it to index.php
RewriteRule . index.php