2017-08-08 44 views
0

我迫使HTTPS和通過的.htaccess剝離WWW:如果/ htaccess的其他人打破內的網址

 RewriteCond %{HTTP_HOST} ^www\. [NC,OR] 
     RewriteCond %{HTTPS} off 
     RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] 
     RewriteRule^https://%1%{REQUEST_URI} [R=301,L,NE] 

這完美的作品。但是,我們需要強制www來自某些IP的訪問者(長篇故事)。爲了做到這一點,我做了:

<If "%{REMOTE_ADDR} = '1.2.3.4' || %{REMOTE_ADDR} = '5.6.7.8' || %{REMOTE_ADDR} = '9.10.11.12' || %{REMOTE_ADDR} = '13.14.15.16.17'"> 
     #Force www and force HTTPS for internal network traffic 
     RewriteCond %{HTTP_HOST} !^www\. [NC,OR] 
     RewriteCond %{HTTPS} off 
     RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] 
     RewriteRule^https://www.%1%{REQUEST_URI} [R=301,L,NE] 
    </If> 
    <Else> 
     #Strip www and force HTTPS for public traffic 
     RewriteCond %{HTTP_HOST} ^www\. [NC,OR] 
     RewriteCond %{HTTPS} off 
     RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] 
     RewriteRule^https://%1%{REQUEST_URI} [R=301,L,NE] 
    </Else> 

這個工程。來自指定IP的訪問者被強制爲https://www.example.com,而所有其他流量則獲得標準https://example.com。主頁返回200 OK。但是,它會使所有內部頁面返回404 Not Found。

整個「破」.htaccess如下。 examdiff證實,這與工作版本之間的唯一區別是存在if/else條件。

該網站在Digital Ocean LAMP液滴上運行Laravel 5.4。

<IfModule mod_rewrite.c> 
    <IfModule mod_negotiation.c> 
     Options -MultiViews 
    </IfModule> 

    RewriteEngine On 

    <If "%{REMOTE_ADDR} = '99.31.174.113' || %{REMOTE_ADDR} = '12.112.227.153' || %{REMOTE_ADDR} = '12.0.107.10' || %{REMOTE_ADDR} = '50.240.55.141'"> 
     #Force www and force HTTPS for internal network traffic 
     RewriteCond %{HTTP_HOST} !^www\. [NC,OR] 
     RewriteCond %{HTTPS} off 
     RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] 
     RewriteRule^https://www.%1%{REQUEST_URI} [R=301,L,NE] 
    </If> 
    <Else> 
     #Strip www and force HTTPS for public traffic 
     RewriteCond %{HTTP_HOST} ^www\. [NC,OR] 
     RewriteCond %{HTTPS} off 
     RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] 
     RewriteRule^https://%1%{REQUEST_URI} [R=301,L,NE] 
    </Else> 


    # Redirect Trailing Slashes If Not A Folder... 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)/$ /$1 [L,R=301] 

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

    # Handle Authorization Header 
    RewriteCond %{HTTP:Authorization} . 
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] 
</IfModule> 

<IfModule mod_deflate.c> 
# Compress HTML, CSS, JavaScript, Text, XML and fonts 
AddOutputFilterByType DEFLATE application/javascript 
AddOutputFilterByType DEFLATE application/rss+xml 
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject 
AddOutputFilterByType DEFLATE application/x-font 
AddOutputFilterByType DEFLATE application/x-font-opentype 
AddOutputFilterByType DEFLATE application/x-font-otf 
AddOutputFilterByType DEFLATE application/x-font-truetype 
AddOutputFilterByType DEFLATE application/x-font-ttf 
AddOutputFilterByType DEFLATE application/x-javascript 
AddOutputFilterByType DEFLATE application/xhtml+xml 
AddOutputFilterByType DEFLATE application/xml 
AddOutputFilterByType DEFLATE font/opentype 
AddOutputFilterByType DEFLATE font/otf 
AddOutputFilterByType DEFLATE font/ttf 
AddOutputFilterByType DEFLATE image/svg+xml 
AddOutputFilterByType DEFLATE image/x-icon 
AddOutputFilterByType DEFLATE text/css 
AddOutputFilterByType DEFLATE text/html 
AddOutputFilterByType DEFLATE text/javascript 
AddOutputFilterByType DEFLATE text/plain 
AddOutputFilterByType DEFLATE text/xml 

# Remove browser bugs (only needed for really old browsers) 
BrowserMatch ^Mozilla/4 gzip-only-text/html 
BrowserMatch ^Mozilla/4\.0[678] no-gzip 
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html 
Header append Vary User-Agent 
</IfModule> 

# BEGIN Expire headers 
<ifModule mod_expires.c> 
    ExpiresActive On 
    ExpiresDefault "access plus 5 seconds" 

    # Add correct content-type for fonts 
    AddType application/vnd.ms-fontobject .eot 
    AddType application/x-font-ttf .ttf 
    AddType application/x-font-opentype .otf 
    AddType application/x-font-woff .woff 
    AddType image/svg+xml .svg 

    # Add a far future Expires header for fonts 
    ExpiresByType application/vnd.ms-fontobject "access plus 1 year" 
    ExpiresByType application/x-font-ttf "access plus 1 year" 
    ExpiresByType application/x-font-opentype "access plus 1 year" 
    ExpiresByType application/x-font-woff "access plus 1 year" 

    ExpiresByType image/x-icon "access plus 2592000 seconds" 
    ExpiresByType image/jpeg "access plus 2592000 seconds" 
    ExpiresByType image/png "access plus 2592000 seconds" 
    ExpiresByType image/gif "access plus 2592000 seconds" 
    ExpiresByType image/svg+xml "access plus 2592000 seconds" 
    ExpiresByType application/x-font-woff "access plus 1 month" 
    ExpiresByType video/ogg "access plus 1 month" 
    ExpiresByType audio/ogg "access plus 1 month" 
    ExpiresByType video/mp4 "access plus 1 month" 
    ExpiresByType video/webm "access plus 1 month" 
    ExpiresByType text/css "access plus 7 days" 
    ExpiresByType text/javascript "access plus 7 days" 
    ExpiresByType application/javascript "access plus 7 days" 
    ExpiresByType application/x-javascript "access plus 7 days" 
    ExpiresByType text/html "access plus 600 seconds" 
    ExpiresByType application/xhtml+xml "access plus 600 seconds" 
</ifModule> 
# END Expire headers 

下面是在/etc/apache2/sites-available/000-default.conf

<VirtualHost *:80> 
    ServerName example.com 
    ServerAlias example.com 
     ServerAdmin [email protected] 
    DocumentRoot /var/www/html/public 
    <Directory /var/www/html/public> 
     RewriteEngine On 
     RewriteBase /var/www/html/public 
     AllowOverride All 
    </Directory> 

    ErrorLog ${APACHE_LOG_DIR}/error.log 
    CustomLog ${APACHE_LOG_DIR}/access.log combined 

</VirtualHost> 

000-default-le-ssl.confdefault-ssl.conf類似這個網站的條目。

看來的.htaccess的這部分可能不被執行:

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

難道這是什麼原因造成的所有內頁返回404,當我使用的if/else條件?它以某種方式終止<IfModule mod_rewrite.c>塊中後續行的執行?

回答

0

永遠不能識別問題,但通過擺脫<If><Else>塊來解決。結果:

#Force www and force HTTPS for internal network traffic 
RewriteCond %{REMOTE_ADDR} ^123\.45\.67\.89 [OR] 
RewriteCond %{REMOTE_ADDR} ^123\.45\.67\.90 [OR] 
RewriteCond %{REMOTE_ADDR} ^123\.45\.67\.91 
RewriteCond %{HTTP_HOST} !^www\. [NC,OR] 
RewriteCond %{HTTPS} off 
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] 
RewriteRule^https://www.%1%{REQUEST_URI} [R=301,L,NE]     


#Strip www and force HTTPS for public traffic 
RewriteCond %{REMOTE_ADDR} !^123\.45\.67\.89 
RewriteCond %{REMOTE_ADDR} !^123\.45\.67\.90 
RewriteCond %{REMOTE_ADDR} !^123\.45\.67\.91  
RewriteCond %{HTTP_HOST} ^www\. [NC,OR] 
RewriteCond %{HTTPS} off 
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] 
RewriteRule^https://%1%{REQUEST_URI} [R=301,L,NE]