ModRewrite Redirect Loop - .htaccess

Basically I have this code on my htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(login.php|signup.php)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteRule !^(login.php|signup.php)$ http://%{HTTP_HOST}/$1 [R=301,L]
It was supposed to send the user to a SSL Connection. But it keeps having a redirect loop. What Am I doing wrong?

You can't use back-references with negative patterns.
RewriteRule !^login\.php$|^signup\.php$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
or this
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !login\.php$
RewriteCond %{REQUEST_URI} !signup\.php$
RewriteRule . http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Related

.htaccess - conditionaly redirect subfolders

I would like to implement followin in .htaccess file:
I have "dictionary", which tells me, which subfolder should be redirected where, i.e.
domain1.com/a -> domain2.com/x
domain1.com/b -> domain2.com/y
and everything else should be redirected to
domain2.com/z
That should be simple enough, but I can not figure it out.
My code:
RewriteEngine on
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ - [env=proto:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ - [env=proto:http]
RewriteCond %{HTTP_HOST} ^(www\.)?domain1.com/a$ [NC]
RewriteRule ^(.*)$ %{ENV:proto}://domain2.com/x [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain1.com/b$ [NC]
RewriteRule ^(.*)$ %{ENV:proto}://domain2.com/y [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain1.com$ [NC]
RewriteRule ^(.*)$ %{ENV:proto}://domain2.com/z [L]
Variable %{HTTP_HOST} only matches domain not request uri.
You can use these rules:
RewriteEngine on
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ - [env=proto:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ - [env=proto:http]
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteRule ^a/?$ %{ENV:proto}://domain2.com/x [L,NC,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteRule ^b/?$ %{ENV:proto}://domain2.com/y [L,NC,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteRule ^ %{ENV:proto}://domain2.com/z [L,NC,R=301]

How to avoid redirect from subdomain in htaccess file?

I am currently using this in my htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But it has the unfortunate effect of redirecting every call to the mobile site (m.example.com) to the main page. How do I make an exception to m.example.com? I don't know how to setup RewriteRule correctly.
You can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^m\. [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
And make sure to clear your browser cache.

htaccess Canonicalization for https url (http://www)

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTPS_HOST} !^www.website.com$ [NC]
RewriteRule ^(.*)$ https://www.website.com/$1 [L,R=301]
I used this to redirect:
http to https
http://www to https://www
both these work fine now. Yet I'm not able to redirect https://website.com to https://www.website.com
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "http\:\/\/example\.com\/" [R=301,L]
I tried this code changing http to https, but still it does not work. Is there anything that I'm doing wrong?
You can use:
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTPS_HOST} !^www.website.com$ [NC]
RewriteRule ^(.*)$ https://www.website.com/$1 [L,R=301]
With [OR]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This code worked for me. Thank you!

Redirecting subdomain to query parameter

I would like to convert URLs like subdomain.domain.fr to domain.fr/index.php/subdomain
I tried with that htaccess :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain.fr$ [NC]
RewriteRule ^(.*)$ /index.php/%1 [L,QSA]
</IfModule>
With this, my site responds with domain.fr but I get a Internal Server Error with subdomain.domain.fr
Any idea on what I'm doing wrong ?
You're probably getting looping error. You can use this code to avoid it:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^([^.]+).domain\.fr$ [NC]
RewriteRule ^ /index.php/%1 [L]

I want to remove the www from url on https using .htaccess

I want to remove the www from https url by .htaccess.
for example
i want to change
https://www.example.com/ to https://example.com/
I have tried many rules
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.come/$1 [L]
and
RewriteCond %{SERVER_PORT} ^443
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule (.*) https://example.com$1 [L,R,QSA]
and
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But nothing work. Please help me
Try:
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteCond %{HTTPS}s/%1 ^(on(s)|offs)/(.+)
RewriteRule ^ http%2://%3%{REQUEST_URI} [L,R=301]
Please make sure if you are using X-Forwarding then try this one:
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://example.com/$1 [R,L]

Resources