Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.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/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
Apache .htaccess正在连接规则_Apache_.htaccess_Mod Rewrite - Fatal编程技术网

Apache .htaccess正在连接规则

Apache .htaccess正在连接规则,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,我有一个.htaccess文件: Options +FollowSymLinks RewriteEngine on RewriteRule ^([a-zA-Z-]+)\/([0-9]*)\/?$ ler.php?categoria=$1&id=$2 这是我的工作 关键是,在应用此规则后,我需要将所有HTTP重定向到HTTPS 所以我写下: Options +FollowSymLinks RewriteEngine on RewriteRule ^([a-zA-Z-]+)

我有一个.htaccess文件:

Options +FollowSymLinks
RewriteEngine on    
RewriteRule ^([a-zA-Z-]+)\/([0-9]*)\/?$ ler.php?categoria=$1&id=$2
这是我的工作

关键是,在应用此规则后,我需要将所有HTTP重定向到HTTPS

所以我写下:

Options +FollowSymLinks
RewriteEngine on    
RewriteRule ^([a-zA-Z-]+)\/([0-9]*)\/?$ ler.php?categoria=$1&id=$2
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
但这不是我应该做的。当我执行HTTP请求时,我得到了以下信息:

什么是只更改协议而不修改url其他部分的正确代码? 我认为我的代码在第一个规则中执行了2个请求,这就生成了错误的url

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
您从原始的
请求_URI
重写,当前是您捕获的内容,请尝试:

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


如果不起作用,重写日志可能会有用。

L
标志必须应用于重写规则。
您可以改为使用此代码

Options +FollowSymLinks
RewriteEngine on

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

RewriteRule ^([^/]+)/([^/]+)/$ /ler.php?categoria=$1&id=$2 [L]
注:

  • 您不需要转义斜杠(
    \/
    可以是
    /
  • 在使用此代码重试之前,您需要清除浏览器的缓存。你的旧规则现在在缓存中,如果你不清除它,你就看不到新代码在工作

只要切换规则的顺序——如果先HTTP,则外部重写为HTTPS,然后内部重写为HTTPS——就已经足够了。如上所述,您需要执行301重定向到HTTPSfirst@CBroe这会产生缓慢的服务器行为。在第3行和HTTPS条件之前,我还有30条类似的规则。服务器在任何情况下都必须解析和测试这些规则。快速检查以确定“嘿,这是不是HTTPS连接?”几乎不需要花费任何费用……我严重怀疑这是否有任何实际的性能影响。@CBroe但该规则需要在使用或不使用HTTPS的情况下执行。如果我先在HTTP上设置条件,这将不起作用。谢谢。
chain
不是个好主意。我提出了另一个答案。
Options +FollowSymLinks
RewriteEngine on

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

RewriteRule ^([^/]+)/([^/]+)/$ /ler.php?categoria=$1&id=$2 [L]