简体   繁体   English

Mod_rewrite将默认路径与www结合在一起。 和https重定向

[英]Mod_rewrite combine default path with www. and https redirect

I'm trying to reduce the number of redirects on our website. 我正在尝试减少我们网站上的重定向次数。 I already managed to combine some of our redirect code (forcing www. and https://), but I also need to add a default path (/en/) in case there is none. 我已经设法合并了一些重定向代码(强制使用www。和https://),但是如果没有,我还需要添加默认路径(/ en /)。

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [R=301,L]

The other bit of code I want to implement is this: 我要实现的另一段代码是:

RewriteRule   "^/$"  /en/

So if there's no path or arguments given, it needs to redirect to /en/. 因此,如果没有给出路径或参数,则需要将其重定向到/ en /。 (Now I do this in PHP, meaning almost every person typing "example.com" gets two redirects, where one should really be enough) (现在,我在PHP中执行此操作,这意味着几乎每个输入“ example.com”的人都会获得两次重定向,其中一个应该确实足够)

My goal is to combine these rules into one single redirect. 我的目标是将这些规则组合为一个重定向。 Is that possible? 那可能吗?

Finally, I also have an internal rewrite rule, which is likely irrelevant for the question above, but I'll add it for completeness: 最后,我还有一个内部重写规则,该规则可能与上面的问题无关,但是为了完整起见,我将其添加:

RewriteRule "(^|/)\." - [F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$/? index.php?rewrite=$1 [QSA]

You can use (in this order): 您可以使用(按此顺序):

RewriteRule ^$  /en/

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

If this is a language test, you can also test the values (ex with: en, fr or de) and change the first line in 如果这是语言测试,则还可以测试值(例如:en,fr或de),并更改其中的第一行

RewriteRule !^(en|fr|de)/  /en/ [NC]

Should work: 应该管用:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) https://www.example.com/en%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [R=301,L]

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

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