我從中學到了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