.htaccess www - 使用动态域名重定向(R301)

时间:2017-08-26 14:21:05

标签: .htaccess redirect web

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

            convertView = mlayoutInflator.inflate(R.layout.panel_details_list_item, null);
            /* setting texts in convertView */
            convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("Card touched","Card touched at " + position);
                mBottomSheetDialog = new BottomSheetDialog(mContext);
                bottomSheetView = mlayoutInflator.inflate(R.layout.panel_more_detail_item,null);
                mBottomSheetDialog.setContentView(bottomSheetView);
                mbottomSheetBehavior = BottomSheetBehavior.from((View) bottomSheetView.getParent());
                bottomSheetInterviewerImage = bottomSheetView.findViewById(R.id.panel_profile_picture);
                bottomSheetInterviewerImage.setImageDrawable(drawableList.get(position));
                mBottomSheetDialog.show();
            }
        });

我的问题是它可能是不同数量的级别...... www。 froschkoenig84。放置。净 www。 froschkoenig84。德

所以当它不是以“www”开头时。然后添加,否则不...

  

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

但它不起作用,......它每次都在添加...所以它的结尾就像...... add "www." each time

如何检查它是否以“www”开头。或不? 如果可能的话,没有单独的条件。 :)

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

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] 

:/

2 个答案:

答案 0 :(得分:1)

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

您可以使用:

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

答案 1 :(得分:0)

worink解决方案:

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]