Redirecting subdomain to query parameter - .htaccess

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]

Related

Redirect Any Domain from non-www to www and to HTTPS without double redirect

I have the following code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]
to redirect from non-www to www
On this question, I found the following code, to make such redirection, and also http to https redirect, without casuing a double redirection:
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [R=301,L]
But I don't want the example.com part, I need a flexible code.
So I tried this code:
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,NE,L]
something in syntax is bad, and it doens't work as expected.
You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
Just use this in your .htaccess:
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
Using %{HTTP_HOST} will grab the host instead of implicitly calling example.com. Make sure you clear your cache before testing this.
EDIT BASED ON COMMENTS
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.(.*)\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%1.%2/$1 [R=301,NE,L]

htaccess redirect https://www.example.net to https://www.example.com/members/

My title says it all. I tried the following, but it only works for the non-https version.
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com/members/ [R=301,L]
Try below rule,
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com/members/ [R=301,L]

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 which allows both http and https - change to allow/redirect to https only

I have inherited the following htaccess, and am having issues getting it to always redirect to https.
Currently it allows for both http://www and https://www, but I'm wanting it to always redirect to https://www (subdomains should always redirect to https://subdomain.example.com)
All my attempts so far have caused 500 errors, so I'm obviously missing something.
AddDefaultCharset UTF-8
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example.com$ [NC]
RewriteCond %{REQUEST_URI} !sub/
RewriteRule (.*) /sub/$1 [L]
You can tweak your rules as:
AddDefaultCharset UTF-8
Options +FollowSymLinks
RewriteEngine On
# handle sub-domains
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.example\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# handle main domain
RewriteCond %{HTTP_HOST} ^example\.com$ [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/sub/ [NC]
RewriteRule (.*) sub/$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