不带www域名跳转到www

      发布在:后端技术      评论:0 条评论
<p>网站域名为了统一性,一般会要求不带www的域名,比如 80zx.com 访问时跳转到 www.80zx.com 这样带www开头的域名</p><p>nginx网站环境伪静态的方式解决方法</p><pre class="brush:bash;toolbar:false">if&nbsp;($host&nbsp;~&nbsp;&#39;^xxxxx.com&#39;){ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;301&nbsp;http://www.xxxxx.com$request_uri; }</pre><p><img src="http://www.80zx.com/uploadfile/ueditor/image/202412/17342289707dddba.png" title="image" alt="image"/></p><p>Apache网站环境伪静态的解决方法<br/></p><pre class="brush:bash;toolbar:false">RewriteEngine&nbsp;on RewriteCond&nbsp;%{HTTP_HOST}&nbsp;^xxxxx.com&nbsp;[NC] RewriteRule&nbsp;^(.*)$&nbsp;http://www.xxxxx.com/$1&nbsp;[L,R=301]</pre><p>IIS10网站环境规则如下<br/></p><pre class="brush:bash;toolbar:false">&lt;configuration&gt; &nbsp;&nbsp;&lt;system.webServer&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;rewrite&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;rules&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;rule&nbsp;name=&quot;Redirect&nbsp;to&nbsp;www&quot;&nbsp;stopProcessing=&quot;true&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;match&nbsp;url=&quot;(.*)&quot;&nbsp;/&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;conditions&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;add&nbsp;input=&quot;{HTTP_HOST}&quot;&nbsp;pattern=&quot;^80zx\.com$&quot;&nbsp;/&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/conditions&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;action&nbsp;type=&quot;Redirect&quot;&nbsp;url=&quot;http://www.80zx.com/{R:1}&quot;&nbsp;redirectType=&quot;Permanent&quot;&nbsp;/&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/rule&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/rules&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;/rewrite&gt; &nbsp;&nbsp;&lt;/system.webServer&gt; &lt;/configuration&gt;</pre><p>PHP程序代码的解决方法</p><pre class="brush:php;toolbar:false">&nbsp;&nbsp;$host&nbsp;=&nbsp;strtolower($_SERVER[&#39;HTTP_HOST&#39;]); &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($host&nbsp;==&nbsp;&#39;xxxxx.com&#39;)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;header(&quot;HTTP/1.1&nbsp;301&nbsp;Moved&nbsp;Permanently&quot;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;header(&quot;Location:&nbsp;https://www.xxxxx.com&quot;);exit; &nbsp;&nbsp;&nbsp;&nbsp;}</pre><p><br/></p>
相关文章
热门推荐