有没有办法在共享主机环境的 Apache 2.2.29 上的 .htaccess 中使用 If..Then..Else?

Is there any way to use If..Then..Else in .htaccess on Apache 2.2.29 on Shared Hosting Environment?

我从这个问答中了解到:How to write "if...else" mod_rewrite statements in .htaccess可以在 .htaccess 中实现 If 语句,但在 Apache 2.4 及更高版本上。

我从这个问答中了解到:htaccess if statement,如何为低于 2.4 版本的 Apache 执行 If 语句。 @julp 的回答几乎解决了我的问题,但我认为我不被允许在共享主机上做“附加的事情”。

Apache 2.2.29 上的 Shared Hosting Environment 上的 .htaccess 中使用 If..Then..Else 是否有任何解决方法?

更新:

按照@Thakkie 的指示,我正在添加我的具体要求。

我正在尝试从一个共享主机帐户托管两个域。我的 .htaccess 文件如下:

<If “%{HTTP_HOST} == ‘domain2.com’”>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ domain2.com-directory/domain2-index.php [QSA,L]
</IfModule>

</If>

<Else>

RequestHeader set X-Prerender-Token "<MyToken>"

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} ^www.domain1.com/domain1-index.php$ [NC]
RewriteRule ^(.*)$ https://www.domain1.com/domain1-index.php [R=301,L]

<IfModule mod_proxy_http.c>
RewriteCond %{HTTP_USER_AGENT} baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_

# Only proxy the request to Prerender if its a request for HTML
RewriteRule ^(?!.*?(\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent|\.ttf|\.woff))(.*) http://service.prerender.io/https://%{HTTP_HOST}/ [P,L]
</IfModule>

# Transfers to www.domain1.com when domain1.com is entered
RewriteCond %{HTTP_HOST} ^domain1.com$ [NC]
RewriteRule ^(.*)$ https://www.domain1.com/ [L,R=301]

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ domain1.com-directory/domain1-index.php [QSA,L]

</Else>

我正在为 domain1.com 分配一组操作,为 domain2.com 分配另一组操作,具体取决于使用 <If><Else>HTTP_HOST

我在我朋友的专用服务器 运行 Apache 2.4 上尝试了上面的 .htaccess 文件,它成功了。

但是,不幸的是,我的共享主机帐户提供商是 运行 Apache 2.2.29,因此不支持 <If></If><Else></Else>

在我的情况下 <If><Else> 的解决方法是什么?

这可能不是最完美的解决方案,但对我有用。

我实际上是在使用 <If><Else> 语句来执行特定于域的操作。

由于 Apache 2.2.29 中不允许使用 <If> 语句,因此我在每个 RewriteRule 之前附加了 RewriteCond %{HTTP_HOST} ^domainname.com$ [NC],我希望它是特定于域的。

因此

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ domain2.com-folder/domain2-index.php [QSA,L]

变成

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^www.domain2.com$ [NC]
RewriteRule ^ domain2.com-folder/domain2-index.php [QSA,L]

以上几行的意思是:IF(请求的文件名不存在)AND(HTTP_HOSTwww.domain2.com)THEN 使用 RewriteRule.

本质上,我使用 RewriteCond 来执行 <If> 运算符的功能,可在 Apache 2.4 或更高版本中使用。

现在,整个 .htaccess 文件:

RequestHeader set X-Prerender-Token "<MyToken>"

RewriteEngine On

RewriteCond %{HTTP_HOST} ^domain2.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^www.domain2.com$ [NC]
RewriteRule ^ domain2.com-folder/domain2-index.php [QSA,L]

# Since I want the HTTPS to be domain specific
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(domain1.com|www.domain1.com)$ [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} ^www.domain1.com/domain1-index.php$ [NC]
RewriteRule ^(.*)$ https://www.domain1.com/domain1-index.php [R=301,L]

<IfModule mod_proxy_http.c>
RewriteCond %{HTTP_USER_AGENT} baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_

# Only proxy the request to Prerender if its a request for HTML
RewriteRule ^(?!.*?(\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent|\.ttf|\.woff))(.*) http://service.prerender.io/https://%{HTTP_HOST}/ [P,L]
</IfModule>

# Transfers to www.domain1.com when domain1.com is entered
RewriteCond %{HTTP_HOST} ^domain1.com$ [NC]
RewriteRule ^(.*)$ https://www.domain1.com/ [L,R=301]

# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^www.domain1.com$ [NC]
RewriteRule ^ domain1.com-folder/domain1-index.php [QSA,L]

P.S。还请前辈指正我的描述,如有不妥之处请指正。