user3842742 user3842742 - 3 months ago 58
Apache Configuration Question

.htaccess HTTPS redirect except one page laravel

I want to redirect all HTTPS requests to HTTP except urls that contain this string:
"/account/buy-premium"

Here is my .htaccess file:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} ^/account/buy-premium [NC]
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/account/buy-premium [NC]
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R,L]

# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]

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


https:// example.com/test-page is redirecting to http:// example.com/test-page (good)

But

https://example.com/account/buy-premium/991 is redirecting to http://example.com/index.php (bad - no redirect is needed here)

I cannot find any solution to prevent redirect for the /buy-premium page

Please help

Answer

You will need to use THE_REQUEST variable instead of REQUEST_URI since REQUEST_URI changes to /index.php by your very last rule.

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{THE_REQUEST} \s/account/buy-premium [NC]
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} !\s/account/buy-premium [NC]
RewriteRule ^ http://%{SERVER_NAME}%{REQUEST_URI} [R,L]

# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]

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