2017-04-24 76 views
1

我从中学到了Q & A:How to write "if...else" mod_rewrite statements in .htaccess它可以在.htaccess中实现If语句,但是在Apache 2.4及更高版本上。有没有什么办法可以使用If..Then..Else在Apache 2.2.29的.htaccess上共享主机环境?

我从此得知Q & - 答:htaccess if statement,如何ipmlement如果语句为Apache不到2.4版本。 @ julp的回答几乎解决了我的问题,但我认为我不能在Shared Hosting上做“追加事情”。

有任何解决方法,以在.htaccess中使用If..Then..Else上Apache就2.2.29共享宿主环境

更新:

按照指示通过@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$1 [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}/$2 [P,L] 
</IfModule> 

# Transfers to www.domain1.com when domain1.com is entered 
RewriteCond %{HTTP_HOST} ^domain1.com$ [NC] 
RewriteRule ^(.*)$ https://www.domain1.com/$1 [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一组操作的,并根据使用<If><Else>HTTP_HOST上domain2.com另一组操作。

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

但是,不幸的是,我的共享托管帐户提供程序正在运行Apache 2.2.29,因此不支持<If></If><Else></Else>

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

+0

如果ELSE指令在2.2上不可用。你需要apache 2.4+来使用它们。 – starkeen

+0

@starkeen是的,我明白了。但有没有解决方法? – Raja

+0

您需要发布精确完整的说明和示例,因为任何解决方法都会针对您的需求进行特定。 – Thakkie

回答

1

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

我实际上使用<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$1 [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}/$2 [P,L] 
</IfModule> 

# Transfers to www.domain1.com when domain1.com is entered 
RewriteCond %{HTTP_HOST} ^domain1.com$ [NC] 
RewriteRule ^(.*)$ https://www.domain1.com/$1 [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] 

附:如果您觉得这是不准确的,我请求老年人请纠正我的描述。

相关问题