Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/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
将http重定向到https,将www重定向到.htaccess中的非www_.htaccess_Redirect_Mod Rewrite_Mod Alias - Fatal编程技术网

将http重定向到https,将www重定向到.htaccess中的非www

将http重定向到https,将www重定向到.htaccess中的非www,.htaccess,redirect,mod-rewrite,mod-alias,.htaccess,Redirect,Mod Rewrite,Mod Alias,首先,我知道在这方面有很多答案,但实际上我没有找到一个有效的答案。这就是我现在在.htaccess文件中看到的,我想说的是它以前工作过,但现在不再工作了 Redirect 301 /unt-de-cacao-de-plaja/filtre/producator/crisnatur/ /ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g Options +FollowSymlinks # Prevent Directoy l

首先,我知道在这方面有很多答案,但实际上我没有找到一个有效的答案。这就是我现在在
.htaccess
文件中看到的,我想说的是它以前工作过,但现在不再工作了

Redirect 301 /unt-de-cacao-de-plaja/filtre/producator/crisnatur/ /ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g

Options +FollowSymlinks

# Prevent Directoy listing
Options -Indexes

# Prevent Direct Access to files
<FilesMatch "(?i)((\.tpl|\.ini|\.log|(?<!robots)\.txt))">
 Require all denied
## For apache 2.2 and older, replace "Require all denied" with these two lines :
# Order deny,allow
# Deny from all
</FilesMatch>

# SEO URL Settings
RewriteEngine On
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/

RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=extension/feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=extension/feed/google_base [L]
RewriteRule ^system/download/(.*) index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

# FORCE HTTPS AND NON WWW
RewriteEngine on

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

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
并且需要重定向到:

https://example.com/ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g

几个问题,按重要性排序:

  • 文件末尾有规范的HTTP到HTTPS和www到非www重定向。通过将它放在文件的末尾,在前端控制器之后,大多数请求都不会处理它这需要靠近
    .htaccess
    文件的开头,在前控制器之前

  • 您应该避免在同一范围内混合来自mod_别名(
    重定向
    )和mod_重写(
    重写规则
    )的重定向。在整个请求过程中,不同的模块在不同的时间执行,尽管它们在配置文件中的顺序明显不同。由于其他重定向需要mod_rewrite,因此应将mod_别名
    Redirect
    指令转换为使用
    RewriteRule

    例如:

    RewriteRule ^unt-de-cacao-de-plaja/filtre/producator/crisnatur/$ /ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g [R=301,L]
    
    RewriteRule ^unt-de-cacao-de-plaja/filtre/producator/crisnatur/$ https://example.com/ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g [R=301,L]
    
  • 您应该在URL重定向中包含规范方案和主机名,以避免在非规范方案主机名处请求“旧”URL时发生多次重定向

    例如:

    RewriteRule ^unt-de-cacao-de-plaja/filtre/producator/crisnatur/$ /ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g [R=301,L]
    
    RewriteRule ^unt-de-cacao-de-plaja/filtre/producator/crisnatur/$ https://example.com/ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g [R=301,L]
    
  • 根据您所说的“大量重定向301”的确切含义,您不应该在
    .htaccess
    中执行此操作,而是在服务器端脚本中进行重定向,一旦您确定请求将是404。这是为了优先考虑正常的网站访问者,而不是你的重定向(每一个请求都会执行)


  • 既然您之前说过这些指令有效,那么我假设在您的系统上使用
    HTTPS
    环境变量是可以的。但请注意,虽然这是相对常见的,但它是非标准的。(这意味着服务器正在使用某种SSL前端/代理。)

    请注意,当请求
    http://www.example.com/,否则,您应该反转这两个规则以避免这种不必要的双重重定向

    重定向到https和非www

    要将所有请求重定向到https和非www,请使用以下代码,而不是以前的代码:

    规范的HTTPS/non-WWW
    非常感谢您的帮助,您的解释修复了https非www重定向的问题@丹尼尔。我将永远帮助需要帮助的人。你可以看到我所有的答案。这是一个非常容易理解和非常有用的你。非常感谢投票和评论我的答案。请看这里的所有内容。这个答案无法说明规范重定向在配置文件中的错误位置。即使您“更正”了这些指令,您仍然需要将这些指令移动到前端控制器之前,否则它将无法重定向大多数URL。感谢您的帮助,这帮助我了解了重定向应该如何实际工作。因为您已经接受了另一个答案,大概是
    %{ENV:HTTPS}
    毕竟是个问题吗?虽然你说过,“它以前工作过,但现在不工作了”?那么,这是不是已经改变了?
    <IfModule mod_rewrite.c>
        RewriteCond %{HTTPS} off [OR]
        RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
        RewriteRule (.*) https://example.com/$1 [L,R=301]
    </IfModule>
    
    RewriteEngine On
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]