gpt4 book ai didi

apache - 如何在 .htaccess 中编写 "if...else"mod_rewrite 语句

转载 作者:行者123 更新时间:2023-12-01 10:05:19 24 4
gpt4 key购买 nike

我已经在 StackOverflow 上搜索过这个问题的答案,看到很多问题都接近尾声,但对我来说还没有任何效果。

我想要我的 .htaccess根据调用它的域应用不同的规则。基本上,如果 subdomain.example.com 被点击,重定向到 google.com。如果命中 localhost,则重定向到 yahoo.com。如果 www.example.com(或 example.com)被点击,重定向到 bing.com。

稍后我将添加实际的重写规则(设置环境变量和 htpasswd 保护),但首先我需要让“if then”部分工作。

我的(非工作)代码如下。任何帮助是极大的赞赏。

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]

编辑:根据下面 Barta 的评论更新(但仍然无效)代码。
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]

最佳答案

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

我的服务器设置了一个环境变量 HTTPS如果通过 HTTPS 连接,尽管有不同的方式来获取它。我的目标是强制 HTTP 身份验证,但仅限于安全连接并将非安全连接重定向到安全位置。

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

关于apache - 如何在 .htaccess 中编写 "if...else"mod_rewrite 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11528634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com