2

这是一个相对标准的问题,但我似乎无法让它在已经进行重写的 URL 上工作。

例如,我有这个网址:

http://example.com/this-is-rewritten/
https://example.com/this-is-rewritten/

应该去:

http://www.example.com/this-is-rewritten/
https://www.example.com/this-is-rewritten/

我想确保它总是在前面是 WWW,如果它不是子域 URL。所以,如果我们有:

http://subdomain.example.com/this-is-rewritten/

那不应该去万维网。这是我到目前为止所拥有的,但它会将您发送到带有查询字符串的底层 URL,而不是相同的“/this-is-rewritten/” URL。此外,应该保留 http 或 https。

RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?:www.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http(?%1s)://%2%3 [R=301,L]
4

2 回答 2

1

尝试:

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^([^.]+)\.([a-z]+)$
RewriteRule ^(.*)$ http://www.%1.%2/$1 [L,R=301]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^([^.]+)\.([a-z]+)$
RewriteRule ^(.*)$ https://www.%1.%2/$1 [L,R=301]

或者如果您只有一个特定的主机:

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{HTTPS}s (?:on(s)|off(s))
RewriteRule ^(.*)$ http%1://www.example.com/$1 [L,R=301]  
于 2013-12-18T01:43:54.347 回答
0

Helicon 的常见问题解答中也有一个常见的解决方案。工作 99.9%

RewriteEngine on

RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]
于 2013-12-18T13:32:09.537 回答