RewriteCond and RewriteRule for www. redirection and https redirection - mod-rewrite

i found following Rewrite Rules, i would like to combine. They are stored in the /etc/apache2/sites-enabled/000-default.conf
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
Unfortunately it is not working together, only one of them is working.
For example:
If i open http://example.com/ it redirects me to https://example.com/
If i open https://www.example.com/ it redirects me to https://example.com/
So far, so good.
But if i open http://www.example.com/ ... it does nothing.
Why?
Edit:
<If "%{HTTP_HOST} == '^www\.example\.com$'">
RewriteRule ^(.*)$ https://example.com/$1 [NC,R=301,L]
</If>
<If "%{SERVER_PORT} != '^443$'">
RewriteRule (.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]
</If>
Also is not working.

Related

Redirect all aliases to main domain [duplicate]

I need to redirect URLs with wrong domains to the correct domain.
Pseudo code example:
if (domain != "www.correctdomain.com")
redirect("www.correctdomain.com")
How can I do this with a .htaccess file?
You can do this with an If directive...
<If "%{HTTP_HOST} != 'www.example.com'">
Redirect / http://www.example.com/
</If>
Or mod_rewrite. See http://httpd.apache.org/docs/current/rewrite/remapping.html
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) http://www.example.com/$1 [L,R,NE]
It's better to make 301 redirect in SEO purposes:
RewriteCond %{HTTP_HOST} ^www.example.com(.*)$ [NC]
RewriteRule ^(.*)$ http://example.com$1 [R=301,L]

Rewrite Condition with if-not Condition

I've tried now many variations but got no usable result.
First issue: If someone requests http://sub.domain.de this have to be rewrite to https://[...].
Second issue: If in the requested uri is /seafdav than there should be no rewrite.
Here my different variations:
Redirect permanent / https://sub.domain.de/
Redirect permanent /seafdav http://sub.domain.de/seafdav/
RewriteRule ^$ /? [L,R=301] [OR]
RewriteRule ^seafdav/$ /seafdav/? [L,R=301]
RewriteCond %{SERVER_NAME} =sub.domain.de [OR]
RewriteCond %{REQUEST_URI} !^/seafdav.$
RewriteRule ^/?(.*) https://sub.domain.de/$1 [R,L]
RewriteCond %{SERVER_NAME} =sub.domain.de [OR]
RewriteCond %{REQUEST_URI} !^/seafdav.$ [NC]
RewriteRule ^/?(.*) https://sub.domain.de/$1 [R,L]
Thanks in advance for your help!
Found now THE solution! :)
<If "%{REQUEST_URI} !~ /seafdav/">
Redirect permanent / https://sub.domain.de/
</If>

Laravel - htaccess redirect from www to non-www and from http to https with request uri goes to index.php

I've tried several solutions but none of them seem to work. I have a Laravel application(public folder is removed) and i wanted to redirect the user to HTTPS and a non-www version of my website
Here is an example of the action i wanted to accomplish
Redirect http://example.com to https://example.com
Redirect http://www.example.com to https://example.com
Redirect https://www.example.com to https://example.com
I was able to accomplish that using the following code on htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example.com$ [NC]
RewriteRule (.*) https://example.com [R=301,L]
Now, since the website has many links, i wanted to redirect the users without losing any text after the domain. Here is an example
Redirect https://www.example.com/electronics/laptops/hp to https://example.com/electronics/laptops/hp
I tried many variations using $ and request_uri but they keep redirecting to https://example.com/index.php. After searching for answers here, the last thing i tried looked something like this, which yielded the same result
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^ https://example.com%1%{REQUEST_URI} [L,R=301,NE]
This seems to redirect any traffic that comes as https://www.example.com/string1/string2/string3 to https://example.com/index.php
So how can i redirect traffic as intended and which is a better practice. Write two rules or combine the rules using If statements.
I would like to use htaccess only methods, no middleware.
I had the same problem
You should put this code on top of .htaccsee file after
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://yourdoamin.com%{REQUEST_URI} [L,R=301,NC]
if you put this after
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
your page redirect to yourdomain.com/index.php
To redirect www requests to non-www without losing the path or the query string this is the right format:
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule (.*) https://example.com/$1 [R=301,L]
If your website is on a server without a reverse proxy, you can easily redirect non-HTTPS requests to HTTPS as following:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
In case there is a reverse proxy or a load balancer you need to check the forwarded headers, depending on the proxy configuration (eg. X-Forwared-Proto)
Try out this one
# Redirect from http to https
RewriteEngine on
# Redirect to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# (http://www.example.com/foo will be redirected to http://example.com/foo)
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]
# to redirect index.php to the root
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
I had the same problem
You should put this code on top of .htaccsee file after
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://yourdoamin.com%{REQUEST_URI} [L,R=301,NC]
if you put this after
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
your page redirect to yourdomain.com/index.php
After spending a few hours searching the web and testing different suggestions, I found the solution.
.htaccess in the public_html
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
.htaccess in the public folder
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Redirect to https
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://yourdomain.com/$1 [R,L]
# Redirect to non www
RewriteCond %{HTTP_HOST} ^www.yourdomain.com$ [NC]
RewriteRule ^(.*)$ https://yourdomain.com/$1 [R=301,L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# This made the trick for me
# Remove index.php from the url
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
</IfModule>
I hope this will work for others too

Redirecting non-www to www in IsapiRewrite

I am trying to redirect calls to http://mydomain.com to http://www.mydomain.com.
Is there a simple way to do it in IsapiRewrite file?
I have tried the following but it is not working:
RedirectRule ^mydomain.com$ www.mydomain.com [R=301]
Also tried this:
RewriteCond %{HTTP_HOST} ^mydomain.com [I]
RewriteRule (.*) http\://www.mydomain.com$1 [I,RP]
Any idea what I am doing wrong.
Thanks!
Here's the code:
RewriteEngine on
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]

htaccess 301 Redirect

I'm trying to 301 redirect
http://www.domain.com/page.html
to
http://subdomain.domain.com/page.html
and tried:
redirect 301 /page.html http://subdomain.domain.com/page.html
The problem is both the domain and the subdomain are pointed to the same dir and that makes the redirect in a way that will never complete.
also tried with no success:
RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^page\.html$ "http\:\/\/subdomain\.domain\.com\/page\.html" [R=301,L]
ok...I figured this out - the second case works - just needs to be placed right after RewriteEngine On:
RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^page.html$ http://subdomain.domain.com/page.html [R=301,L]
and this could be used for multiple rules under one condition:
RewriteCond %{HTTP_HOST} !^(www\.)?domain.com$ [NC]
RewriteRule .* - [S=2]
RewriteRule ^page.html$ http://subdomain.domain.com/page.html [R=301,L]
RewriteRule ^page-2.html$ http://subdomain.domain.com/page-2.html [R=301,L]

Resources