1

I have removed public word from url by applying following code in .htaccess

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

It is working undoubtedly. But also trying to set permanent redirection http to https. Using following code in .htaccess:

RewriteEngine on
RewriteCond %{HTTPS} !^on
RewriteRule ^(.*)$ public/$1 [L]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Please tell me where is the issue? It should remove public folder from url as well as permanent redirection to https URL

1 Answers1

3

RewriteCond applies only to the next RewriteRule

The RewriteCond directive defines a rule condition. One or more RewriteCond can precede a RewriteRule directive. The following rule is then only used if both the current state of the URI matches its pattern, and if these conditions are met.

So in your case, you must move RewriteCond in front of the second RewriteRule

RewriteRule ^(.*)$ public/$1 [L]
RewriteCond %{HTTPS} !^on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Although, since you want to enforce HTTPS, you should move that to the front, e.g.

RewriteCond %{HTTPS} !^on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteRule ^(.*)$ public/$1 [L]

When everything works as it should, you may replace R with R=301. Never test with R=301.

Community
  • 1
  • 1
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198