2017-08-16 14 views
0

私のアプリはherokuでホストされており、私はCakephp APIを使用しています。 https://wwwにリダイレクトしてください。 wwwを強制する。サブドメイン。.htaccesとcakephpを使ってhttpからhttpsにドメインをリダイレクトする方法

そして、2つのドメインが同じアプリを指しています。

私は、次のコードを持って/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] 

で、次のコードを持っていますが、それはdoesnの仕事。ただ、リクエストが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を追加する必要があります。

私はこれがあなたを助けてくれることを願っています。あなたはまた、

を 'www' を追加することができます

ちょうど私たちは、.htaccessファイルを使用してHerokuのでwww.domainするドメインをリダイレクトすることはできません。この

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> 

そしてシュルサーバー(または仮想ホスト)とする(HTTPS用)ポート443を聞く

<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] 

おかげ

関連する問題