Vergil Penkov
Vergil Penkov

Reputation: 355

.htaccess - target everything EXCEPT the index (with using clean URLs)

My code is this:

<IfModule mod_rewrite.c>

RewriteEngine On

# Detect Protocol
RewriteCond %{HTTPS} =on
RewriteRule ^ - [env=proto:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^ - [env=proto:http]

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

# Force WWW and correct protocol
#RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ %{ENV:PROTO}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteRule ^\.htaccess$ - [F]
RewriteRule ^php\.ini$ - [F]

RewriteRule ^cgi-bin - [R=404]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(jpg|jpeg|gif|png|js|css|swf)$
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

I want to force SSL everywhere EXCEPT on the homepage. Therefore, I need to see if the requested address is equal to "/"

I can't quite figure it out. I tried all of the following but I seem to need a htaccess guru:

RewriteCond $1 !^(/)
RewriteCond %{REQUEST_URI} !(/)
RewriteCond %{REQUEST_URI} !(/$)

...and a few others. Pretty sure I need to add it after

RewriteCond %{HTTPS} !=on

but I'm stuck

Upvotes: 1

Views: 78

Answers (1)

anubhava
anubhava

Reputation: 785128

You can use:

RewriteEngine On

# Detect Protocol
RewriteCond %{HTTPS} on
RewriteRule ^ - [env=proto:https]
RewriteCond %{HTTPS} off
RewriteRule ^ - [env=proto:http]

# Force SSL except for home page
RewriteCond %{HTTPS} off
RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Force WWW and correct protocol
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ %{ENV:PROTO}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteRule ^\.htaccess$ - [F]
RewriteRule ^php\.ini$ - [F]
RewriteRule ^cgi-bin - [R=404]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(jpe?g|gif|png|js|css|swf)$ [NC]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

Upvotes: 1

Related Questions