Mod_rewrite to switch HTTP to HTTPS - mod-rewrite

I am trying to use a Bluehost supplied SSL wildcard certificate to switch one subdomain to HTTPS.
The web root contains many subdomains, and I only want to affect the test. subdomain.
Going into the web root, I've written the following .htaccess file:
RewriteCond %{SERVER_NAME} ^test.mydomain.com
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}$1
Yet hitting http://test.mydomain.com/admin/index.php does not redirect me to https://test.mydomain.com/admin/index.php.
Even cutting out the Condition of SERVER_NAME, it still doesn't work.
Is my rewrite rule bad?

Well, in the first place I was in the wrong .htaccess file. I needed to go to the directory for my subdomain.
Then, simply switching it from HTTP to HTTP apparently causes some kind of redirect to www. behavior.
I had to
A) Preserve the existing rules that were not intended for test.
B) Add the test. rules.
It added up to a lot.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^test.example.com$
RewriteRule ^index\.php$ - [L]
RewriteCond %{HTTP_HOST} !^test.example.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} ^test.example.com$
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} ^test.example.com$
RewriteCond %{REQUEST_URI} !^/test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /test%{REQUEST_URI}
RewriteCond %{HTTP_HOST} ^test.example.com$
RewriteRule ^(/)?$ test/admin/index.php [L]

Related

How to convert .htaccess to web.config?

I am realy lost here. I am used to work with linux server so I use .htaccess
Now I need to develop a project in windows server and I need to use some rewrite rules, etc. So I need to set the web.config file.
So I need help to convert my .htaccess rules to web.config
This are the rules:
RewriteEngine On
RewriteRule ^box/camp(.+)?/?$ /_box/access_box_files/camp$1
RewriteRule ^box/merchant(.+)?/?$ /box/access_box_files/merchant$1
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url_tab=$1 [L,QSA]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
UPDATE
RewriteRule ^box/camp(.+)?/?$ /_box/access_box_files/camp$1
In this case I need that whenever the URL is "/box/camp" there is access to "/_box/access_box_files/cam" and that all the information that comes in front of "(.+)?" "$1" is included. Without changing the URL in the browser, or forwarding the user.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url_tab=$1 [L,QSA]
In this case, any information that comes in the URL must be collected in a variable.
For example:
https:// website dot com/tab1/tab2/tab3
I will get the information in: $_GET['url_tab'] which would have the information: "tab1/tab2/tab3".
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
In this case it is to force the URL to be loaded with https.
Thank you all very much.

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

Ignore HTTPS redirection for two specific URLS

Hi I have a loadbalancer with and SSL certificate assigned and two webservers. The loadbalancer ports HTTP traffic to port 80 and HTTPS traffic to port 81. All requests to HTTP are redirected using apache rewrite using the HTTP:X-Forwarded-Proto header. This works fine, but now we need to ignore two particular REQUEST_URIs but I cannot seem to get this to work as expected.
The application is using a basic MVC structure and so all URIS are sent to the index.php file, but when I try to ensure the two URLs are only on HTTP, the combinations I have tried either end in the route not being found or stuck in a redirect loop.
Below is the rewrite and combinations I have tried.
RewriteEngine On
# This causes route to not be found
#RewriteCond %{REQUEST_URI} ^/uritoignore/one$ [OR]
#RewriteCond %{REQUEST_URI} ^/seconduritoignore/two$
#RewriteRule ^(.*)$ http://%{HTTP_HOST}/index.php?url=$1 [QSA,L]
# Continuous redirect loop
#RewriteCond %{REQUEST_URI} ^/uritoignore/one$
#RewriteRule ^(.*)$ http://%{HTTP_HOST}/uritoignore/one [QSA,L]
# Redirect rest of traffic to HTTPS if not already
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Any advice would be greatly appreciated.
Thanks
In the end I realised what was staring me in the face. I simply needed to exclude the paths from the redirection, and also the index.php to stop this from being redirected when it is rewritten at the end. The final configuration was as follows:
RewriteEngine On
# Redirect rest of traffic to HTTPS if not already
RewriteCond %{REQUEST_URI} !^/uritoignore/one
RewriteCond %{REQUEST_URI} !^/seconduritoignore/two
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

mod_rewrite: don't rewrite any subdomains

Is there a way to exempt any requests to a subdomain in mod_rewrite? Right now, I have a one-page app that's redirecting everything to index.html.
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]
</IfModule>
But I want to make sure that calls to subdomain.mydomain.com are permanently exempt from being rewritten.
You can use a condition on the subdomain and the - substitution to stop the rewriting. From http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule
There is a special substitution string named '-' which means: NO
substitution! This is useful in providing rewriting rules which only
match URLs but do not substitute anything for them.
Try
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.mydomain\.com$ [NC]
RewriteRule .* - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]

Need mod_rewrite condition and rule to replace ".com" with ".org" on 1 virtual host

I have a series of mod_rewrite directives (shown below as Code 1) that run every server request through a custom PHP app.
Code 1
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app/core.php [L,NC,QSA]
Before I get to the last step in the mod_rewrites, I need to change any request to mydomain.com to mydomain.org. Code 2 below shows what I am thinking but it does not work. The request gives me 500 Internal Server error.
Code 2
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteRule ^ http://mydomain.org%{REQUEST_URI}
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app/core.php [L,NC,QSA]
Can someone offer a suggestion? Thanks
I asume your site is reachable by two domains, but you want all request to your site redirected to one of them. Then the correct code is:
RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://mydomain.org/$1 [R=permanent,L]
You should place that code directly after the redirect line.
The second line makes sure that there is no redirect if no Host is given (to prevent a redirect loop in that case).
The [R=permanent,L] in the third line makes it a permanent redirect and prevents any further rule processing. The other rules will be processed once the redirect has taken place.
The full file would then be:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://mydomain.org/$1 [R=permanent,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app/core.php [L,NC,QSA]
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://mydomain.org/$1 [R=permanent,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app/core.php [L,NC,QSA]

Resources