我从中学到了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>
的解决方法是什么?
如果ELSE指令在2.2上不可用。你需要apache 2.4+来使用它们。 – starkeen
@starkeen是的,我明白了。但有没有解决方法? – Raja
您需要发布精确完整的说明和示例,因为任何解决方法都会针对您的需求进行特定。 – Thakkie