首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有动态域名的.htaccess www -重定向(R301)

具有动态域名的.htaccess www -重定向(R301)
EN

Stack Overflow用户
提问于 2017-08-26 14:21:04
回答 2查看 43关注 0票数 1

我想重定向3级或4级域以添加“www”:

代码语言:javascript
复制
DirectoryIndex index.php

RewriteEngine on
RewriteRule ^$ /home/ [R=301,L]


RewriteRule ^(?!www[\.]) http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

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

我的问题是这可能是一个不同的水平..。www。froschkoenig84。放好了。net。froschkoenig84。de

所以当它不是以“www”开头的时候。那就加一句,否则.

RewriteRule ^(?) http://www.%{HTTP_HOST}%{REQUEST_URI} R=301,L

但不起作用..。它每次都在增加..。所以结局就像..。

我怎样才能检查它是否以“www”开头。还是不想?如果可能的话,那就没有单独的条件。:)

我还为.NET (web.config)创建了类似的重写规则:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <system.webServer>
  <rewrite>
   <rules>
    <rule name="RedirectToHome" stopProcessing="true">
     <match url="^$" />
     <conditions trackAllCaptures="true">
      <add input="{CACHE_URL}" pattern="^(https?)://" />
     </conditions>
     <action type="Redirect" redirectType="Permanent" url="{ToLower:{C:1}://{HTTP_HOST}/home/}" />
    </rule>
    <rule name="RedirectToWWW" stopProcessing="true">
     <match url="(.*)" ignoreCase="false"  />
     <conditions>
      <add input="{CACHE_URL}" pattern="^(https?://)(?!www[\.])" />
     </conditions>
     <action type="Redirect" url="{ToLower:{C:1}www[\.]{R:1}}" redirectType="Permanent" />
    </rule>
    <rule name="RewriteToParams" stopProcessing="true">
     <match url="^(.*)$" ignoreCase="false" />
     <conditions>
      <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
      <add input="{URL}" pattern="^/favicon.ico$" ignoreCase="false" negate="true" />
     </conditions>
     <action type="Rewrite" url="{ToLower:index.php?p=/{R:1}}" appendQueryString="true" />
    </rule>
   </rules>
  </rewrite>
 </system.webServer>
</configuration>

:/

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-08-26 14:26:37

域名不是RewriteRule中的测试uri的一部分。

您可以使用:

代码语言:javascript
复制
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
票数 1
EN

Stack Overflow用户

发布于 2017-08-26 15:35:09

worink解决方案:

代码语言:javascript
复制
DirectoryIndex index.php

RewriteEngine on
 #RedirectToHome
  RewriteRule ^$ /home/ [R=301,L]

 #RedirectToWWW
  RewriteCond %{HTTP_HOST} !^www\. [NC]
  RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]

 #RewriteToParams
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?p=/$1 [QSA]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45896387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档