samedi 26 août 2017

.htaccess www - redirect (R301) with dynamic domainname

I want to redirect 3rd or 4th level domain to add "www.":

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] 

My problem is that it could be a different number of levels... www . froschkoenig84 . bplaced . net www . froschkoenig84 . de

So when it's not starting with "www." then add, else not...

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

But it doesn't work, ... it's adding each time... so it's ending like... add "www." each time

How can I check if it starts with "www." or not? If possible, then without a separate condition. :)

I created similar rewrite rules for .NET (web.config), too:

<?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>

:/




Aucun commentaire:

Enregistrer un commentaire