RewriteCond: redirecting all request from specific domains - mod-rewrite

I'm trying to write a RewriteCond that will redirect a number of domains to a given URL:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain1.com$ [OR]
RewriteCond %{HTTP_HOST} ^(www.)?domain2.com$
RewriteRule ^(/)?$ http://www.google.com/ [L]
This is almost working fine. When visiting my domain, I'm redirected to google.com. But when visiting domain1.com/a-sub-folder, it does not match my RewriteCond.
What could I add to the RewriteCond to make them match the above URL?
EDIT1:
Also, is it possible to add some HTTP 301 response to the redirect?

^(/)?$ in the rewrite rule match only an empty path, I replaced it with .*
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain1.com$ [OR]
RewriteCond %{HTTP_HOST} ^(www.)?domain2.com$
RewriteRule .* http://www.google.com/ [L,R=301,NC]

Related

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

mod_rewrite: 2 files should always be SSL, the rest always HTTP

I want to ensure that a small number of URLs in my application are always served over SSL; the rest should always be served over HTTP.
My current mod_rewrite ruleset goes like this:
RewriteCond %{HTTPS} off
RewriteRule ^/?account.html$ https://example.com/account.html [L]
RewriteRule ^/?checkout.html$ https://example.com/checkout.html [L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !/account.html$
RewriteCond %{REQUEST_URI} !/checkout.html$
RewriteRule (.*) http://example.com/$1 [L]
The first file in each RewriteCond works OK (account.html in this example). The 2nd file doesn't work - the server is "redirecting in a way that will never complete".
Any ideas on how to make this work? In production there'll be more than 2 SSL-only URLs, likely 7 - 10.
Thanks
You must use R flag to redirect url
RewriteEngine On
# redirect account.html or checkout.html (if http) to https
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/(account|checkout)\.html$ [NC]
RewriteRule ^ https://%{HTTP_HOST}/%1.html [R,L]
# redirect other urls (if https) to http
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(account|checkout)\.html$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R,L]
Note: i used R flag, which performs a 302 redirect by default. Feel free to replace [R,L] by [R=301,L] when it works (301 redirect is a permanent redirect and is cached by browsers)

Force https on all pages except for 1 page with a specific value in a Query String Parameter

I have already searched this site but my workaround does not work.
Basically, I need all pages to have https turned on except for 1 page that has a url like this:
domain.com/begin/index.php?pageid=130&usertype=1&building=2
If the page has the Querystring parameter pageid=130 I do not want https turned on.
My .htaccess file looks like this:
RewriteEngine On
#HTTPS OFF on pageid 130
RewriteCond %{HTTPS} on
RewriteCond %{QUERY_STRING} (^|&)pageid=(130)($|&)
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
#FORCE HTTPS ON ALL PAGES except for pageid 130
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} !(^|&)pageid=(130)($|&)
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
LOL! Small mistake but hard to spot: you still redirect to https ;). Just remove the 's'.
Otherwise may redirect for sure and stop the rewrite process = Last = L.
RewriteEngine On
# HTTPS Off on pageid 130
RewriteCond %{HTTPS} On
RewriteCond %{QUERY_STRING} (^|&)pageid=130($|&)
# (!) Remove the "s":
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [QSA,NC,L]
# Force HTTPS on all pages but the one QS 'pageid=130'
RewriteCond %{HTTPS} Off
RewriteCond %{QUERY_STRING} !(^|&)pageid=130($|&)
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [QSA,NC,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]

How can I conditionally redirect to https using mod_rewrite?

I would like to redirect to https using mod_rewrite only if certain conditions are met:
If the URL does NOT contain the word 'administrator'
AND the URL DOES contain the string 'xyz' (in any part of the URL, including the querystring)
This does not seem to work:
RewriteCond %{REQUEST_URI} xyz [NC,OR]
RewriteCond %{QUERY_STRING} xyz [NC]
RewriteCond %{REQUEST_URI} !administrator [NC]
ReWriteCond %{HTTPS} != on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R,L]
Try this rule:
RewriteCond %{THE_REQUEST} !administrator
RewriteCond %{THE_REQUEST} xyz
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Testing the request line in THE_REQUEST is easier as it contains both the path and query. But make sure your xyz is not part of the method or HTTP version.
I ended up using a coding solution as I could not get it to work with mod_rewrite. :(

Resources