2017-08-16 43 views
0

我的應用託管在heroku上,並且我正在使用Cakephp API。我希望它重定向到https://www。並執行www。子域。如何使用.htacces和cakephp將域名從http重定向到https

而且,他們有兩個域指向同一個應用程序。

我有如下代碼,在/app/.htaccess

RewriteEngine on 

RewriteBase/

#apply if no https 
RewriteCond %{HTTPS} off  
#Ignore when is local env or any staging env 
RewriteCond %{HTTP_HOST} !^local(.*) [NC] 
RewriteCond %{HTTP_HOST} !^(.*)heroku(.*) [NC] 

RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301] 

#apply if https 
RewriteCond %{HTTPS} on 
#Ignore when is local env or any staging env or subdoamin is www. 
RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteCond %{HTTP_HOST} !^local(.*) [NC] 
RewriteCond %{HTTP_HOST} !^(.*)heroku(.*) [NC] 
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301] 

#default from cakephp 
RewriteRule ^$ webroot/ [L] 
RewriteRule (.*) webroot/$1 [L] 

我有如下代碼,在/app/webroot/.htaccess

RewriteEngine On 

RewriteBase/

#redirect any request from .poa.br to .com.br 
RewriteCond %{HTTP_HOST} ^(.*)example\.poa\.br$ [NC] 
RewriteRule ^(.*)$ https://www.example.com.br/$1 [NE,R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule^index.php [L] 

但它不」工作。就在如下請求重定向到https://www.example.com.br

https://www.example.com.br 
https://www.example.poa.br 
http://www.example.poa.br 

此選項去域,但沒有HTTPS前綴。

http://www.example.com.br 

其他選項不起作用(返程DNS_PROBE_FINISHED_NXDOMAIN):

https://example.com.br 
http://example.com.br 
https://example.poa.br 
http://example.poa.br 

回答

0

你好確保重寫模塊是在此之前的作品。

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.yourwebsite.com/$1 [R,L] 

並且在您的基礎URL中,您需要向http添加(s)。

我希望這可以幫助你。

您還可以添加的 'www'

只需添加這

RewriteCond %{HTTP_HOST} !^www\. 
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L] 
+0

嗨Konjesh!它沒有工作。 我相信重寫模塊已打開,因爲我在測試中創建了一些循環。 –

+0

您的建議結果與我的代碼相同。 –

0

改變你的CakePHP項目文件夾htaccess文件是這樣的:

<IfModule mod_rewrite.c> 
    RewriteEngine on 

    RewriteCond %{HTTPS} !=on 
    RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L] 

    RewriteCond %{QUERY_STRING} ^(.*)http(\:|\%3A)(.*)$ 
    ReWriteRule .* - [F]  

    RewriteRule ^$ webroot/ [L] 
    RewriteRule (.*) webroot/$1 [L] 
</IfModule> 

而且是河畔您的服務器(或虛擬主機)監聽的端口443(用於HTTPS)

<VirtualHost *:80> 
    ServerAdmin [email protected] 
    ServerName subdomain.example.br 
    ServerAlias subdomain.example.br 
    DocumentRoot /var/www/subdomain 
    <Directory /var/www/subdomain/> 
     Options -Indexes +FollowSymLinks +MultiViews 

     AllowOverride All 
     Order Allow,Deny 
     Allow from All 
    </Directory> 

    ServerSignature Off 
    ErrorLog /var/log/apache2/error.log 
</VirtualHost> 

<VirtualHost *:443> 
    ServerAdmin [email protected] 
    ServerName subdomain.example.br 
    ServerAlias subdomain.example.br 
    DocumentRoot /var/www/subdomain 
    <Directory /var/www/subdomain/> 
     Options -Indexes +FollowSymLinks +MultiViews 

     AllowOverride All 
     Order Allow,Deny 
     Allow from All 
    </Directory> 
    SSLEngine on 
    SSLProtocol all -SSLv2 
    SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM 

    SSLCertificateFile /[path to your CRT file] 
    SSLCertificateKeyFile /[path to your PEM file] 
    SSLCertificateChainFile [path to your CRT file] 
    SSLCACertificatePath /etc/ssl/certs/ 
    SSLCACertificateFile /[path to your CRT file] 
    ErrorLog /var/log/apache2/error.log 
</VirtualHost> 
0

我在/app/webroot/.htaccess中做了所有配置

有一個技巧來確定對Heroku的請求是否爲https。

按照代碼:

RewriteEngine On 
RewriteBase/

#Identify if it is https 
RewriteCond %{HTTP:X-Forwarded-Proto} !https 

#ignore local env 
RewriteCond %{HTTP_HOST} !^local(.*) [NC] 

#apply redirect in all subdomain 
RewriteCond %{HTTP_HOST} ^(.*)example\.com\.br$ [NC] 
RewriteRule ^(.*)$ https://example.com.br/$1 [NE,R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule^index.php [L] 

我們決定將重定向所有請求裸域,但如果你想改變到www您coult執行後續命令。

RewriteRule ^(.*)$ https://www.example.com.br/$1 [NE,R=301,L] 

感謝

相關問題