如果htaccess中的/ else会破坏内部URL

时间:2017-08-08 12:36:48

标签: apache .htaccess mod-rewrite

我强迫https并通过.htaccess剥离www:

        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。

整个&#34;破碎&#34; .htaccess在下面。 examdiff确认这个和工作版本之间的唯一区别是存在if / else条件。

该网站在Digital Ocean LAMP Droplet上运行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 example@example.com
    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]

当我使用if / else条件时,这会导致所有内部页面返回404吗?它是以某种方式终止<IfModule mod_rewrite.c>块中后续行的执行吗?

1 个答案:

答案 0 :(得分: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]