本文介绍了如何写"如果...否则"在.htaccess mod_rewrite的声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经冲刷计算器的答案,这一点,看到许多问题接近,但没有什么工作对我来说还没有。

我想有我的基于域称之为的.htaccess 适用不同的规则。基本上,如果subdomain.example.com被击中,重定向到google.com。如果本地主机被击中,重定向到yahoo.com。如果www.example.com(或example.com)被击中,重定向到bing.com。

我会加在实际重写规则(设置环境变量和htpasswd的保护)之后,但首先我需要获得如果当时的部分工作。

我的(非工作)code是如下。任何帮助是极大的AP preciated。

  RewriteEngine叙述上
的RewriteCond%{HTTP_HOST} ^子域\ .EXAMPLE \ .COM $
重写规则http://google.com/ [L,R = 301]

的RewriteCond%{HTTP_HOST} ^本地主机$
重写规则http://yahoo.com/ [L,R = 301]

的RewriteCond%{HTTP_HOST} ^(WWW \)?例如\ .COM $
重写规则http://bing.com/ [L,R = 301]
 

编辑:更新(但仍非工作)code基于以下巴塔的评论

  RewriteEngine叙述上

的RewriteCond%{HTTP_HOST} ^子域\。域\ .COM $
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则? -  [S = 1]
重写规则^(。*)$ /index.php/$1 [L]

的RewriteCond%{HTTP_HOST} ^本地主机$
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则? -  [S = 7]
重写规则^(。*)$ /subdir/www/index.php/$1 [L]
重写规则^  -  [E = MYSQL_DB_HOST:本地主机]
重写规则^  -  [E = MYSQL_DB_N​​AME:XXX]
重写规则^  -  [E = MYSQL_USERNAME:XXX]
重写规则^  -  [E = MYSQL_PASSWORD:XXX]
重写规则^  -  [E = BASE_URL:XXX]
重写规则^  -  [E =环境:XXX]

的RewriteCond%{HTTP_HOST} ^例如\ .COM $
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则? -  [S = 1]
重写规则^(。*)$ /index.php/$1 [L]
 

解决方案

您可能会,如果你使用的是Apache 2.4发现这很有用。

我的服务器上设置一个环境变量 HTTPS 如果通过HTTPS连接,但也有不同的方式来接起来就可以了。我的目标是迫使HTTP验证,但只能在一个安全的连接,并重定向到安全的地方非安全连接。

 <如果%{} HTTPS =='对'>
  AuthName指令的文件被保护,输入您的正常浏览网页的电子邮件和密码
  与AuthType基本
  的AuthUserFile /home/sites/mysite.co.uk/.htpasswd
  需要有效的用户
< /如果>
<否则>
  RewriteEngine叙述上
  重写规则^ $ HTTPS(*)://%{HTTP_HOST}%{REQUEST_URI} [L,R = 301]
< / ELSE>
 

I've scoured StackOverflow for an answer to this and seen many questions come close, but nothing is working for me yet.

I'd like to have my .htaccess apply different rules based on the domain calling it. Basically, if subdomain.example.com is hit, redirect to google.com. If localhost is hit, redirect to yahoo.com. If www.example.com (or example.com) is hit, redirect to bing.com.

I'll be adding in actual rewrite rules (setting environment variables and htpasswd protection) later, but first I need to get the "if then" part working.

My (non-working) code is below. Any help is greatly appreciated.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$
RewriteRule http://google.com/ [L,R=301]

RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule http://yahoo.com/ [L,R=301]

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule http://bing.com/ [L,R=301]

EDIT: Updated (but still non-working) code based on Barta's comment below.

RewriteEngine On

RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? - [S=1]
RewriteRule ^(.*)$ /index.php/$1 [L]

RewriteCond %{HTTP_HOST} ^localhost$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? - [S=7]
RewriteRule ^(.*)$ /subdir/www/index.php/$1 [L]
RewriteRule ^ - [E=MYSQL_DB_HOST:localhost]
RewriteRule ^ - [E=MYSQL_DB_NAME:XXX]
RewriteRule ^ - [E=MYSQL_USERNAME:XXX]
RewriteRule ^ - [E=MYSQL_PASSWORD:XXX]
RewriteRule ^ - [E=BASE_URL:XXX]
RewriteRule ^ - [E=ENVIRONMENT:XXX]

RewriteCond %{HTTP_HOST} ^example\.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? - [S=1]
RewriteRule ^(.*)$ /index.php/$1 [L]
解决方案

You might find this useful if you are using Apache 2.4.

My server sets an environment variable HTTPS if connecting via HTTPS, though there are different ways to pick up on it. My goal was to force HTTP authentication, but only on a secure connection and redirect non-secure connections to the secure location.

<If "%{HTTPS} == 'on'">
  AuthName "Files are protected, enter your normal website email and password"
  AuthType Basic
  AuthUserFile /home/sites/mysite.co.uk/.htpasswd
  Require valid-user
</If>
<Else>
  RewriteEngine On
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</Else>

这篇关于如何写&QUOT;如果...否则&QUOT;在.htaccess mod_rewrite的声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:11