user3387359
user3387359

Reputation: 71

htaccess rewrite url to https and remove filename

I'm using symfony2 framework which is calling app.php file for every page which I remove with htaccess mod rewrite, I'm trying to write htaccess to redirect two pages (with dynamic urls) to https and the rest of the site to regular http, this is where I stand as for now :

<IfModule mod_rewrite.c>
    RewriteEngine on

    RewriteCond %{HTTPS} !=on
    RewriteRule ^(payment|withdraw)(/|$)  https://%{HTTP_HOST}%{REQUEST_URI}  [R=301,L,QSA]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ app.php/$1 [QSA,L]

    #RewriteCond %{HTTPS} =on
    #RewriteRule !^(payment|withdraw)(/|$)  http://%{HTTP_HOST}%{REQUEST_URI}  [R=301,L,QSA]

</IfModule>

this works for adding the https to the specific pages, but if I add the commented out lines to redirect the rest of the pages to regular http it breaks the first part and than http://www.example.com/payment/item_1 becomes http://www.example.com/app.php/payment/item_1 instead of https://www.example.com/payment/item_1

Upvotes: 2

Views: 641

Answers (1)

anubhava
anubhava

Reputation: 785551

Reorder your rules as you must keep redirect rules before internal rewrite ones:

<IfModule mod_rewrite.c>
    RewriteEngine on

    RewriteCond %{HTTPS} off
    RewriteCond %{THE_REQUEST} /(payment|withdraw)[/\s]
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]

    RewriteCond %{HTTPS} on
    RewriteCond %{THE_REQUEST} !/(payment|withdraw)[/\s]
    RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]

    RewriteCond %{REQUEST_FILENAME} !-f    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ app.php/$1 [L]

</IfModule>

Upvotes: 1

Related Questions