- Apache modrewrite redirects to http with 302 instead of https. Redirecting http traffic to https in Apache without using modrewrite. Hot Network Questions.
- To automatically redirect all requests from HTTP to HTTPS you can use Apache's modrewrite module. This is a good solution to force your site visitors to use SSL. Besides this, you can also apply this solution on most shared hosting plans where you have access to the htaccess file.
- How to Redirect HTTP to HTTPS on an Apache Server. There are two primary ways that you could redirect HTTP to HTTPS in Apache: You could use modrewrite in Apache, or; You can use the more secure virtual host redirect method. We always think it’s best to go with the more secure methods.
Tutorial
Introduction
In this tutorial, we will activate and learn how to manage URL rewrites using Apache 2’s mod_rewrite
module. This module allows us to rewrite URLs in a cleaner fashion, translating human-readable paths into code-friendly query strings or redirecting URLs based on additional conditions.
Apache htaccess https mod-rewrite http. Improve this question. Follow edited Feb 22 '13 at 21:43. The Apache module modrewrite is a very powerful and sophisticated module which provides a way to do URL manipulations. With it, you can do nearly all types of URL rewriting that you may need. It is, however, somewhat complex, and may be intimidating to the beginner.
This guide is split into two parts. The first sets up an example website and covers a simple rewrite example. The second part contains two more in-depth examples of commonly-used rewrite rules.
Prerequisites
To follow this tutorial, you will need:
- One Ubuntu 16.04 server set up with this initial server setup tutorial, including a sudo non-root user and firewall.
- Apache 2 installed on your server by following Step 1 of How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 16.04.
Step 1 — Enabling mod_rewrite
First, we need to activate mod_rewrite
. It’s available but not enabled with a clean Apache 2 installation.
This will activate the module or alert you that the module is already enabled. To put these changes into effect, restart Apache.
mod_rewrite
is now fully enabled. In the next step we will set up an .htaccess
file that we’ll use to define rewrite rules for redirects.
Step 2 — Setting Up .htaccess
An .htaccess
file allows us to modify our rewrite rules without accessing server configuration files. For this reason, .htaccess
is critical to your web application’s security. The period that precedes the filename ensures that the file is hidden.
Note: Any rules that you can put in an .htaccess
file can be also put directly into server configuration files. In fact, the official Apache documentation recommends using server configuration files instead of .htaccess
because Apache processes it faster that way.
However, in this simple example, the performance increase will be negligible. Additionally, setting rules in .htaccess
is convenient, especially with multiple websites on the same server. It does not require a server restart for changes to take effect and it does not require root privileges to edit those rules, simplifying maintenance and and making changes possible with unprivileged account. Some popular open-source software, like Wordpress and Joomla, often relies on an .htaccess
file for the software to modify and create additional rules on demand.
We will need to set up and secure a few more settings before we can begin.
By default, Apache prohibits using an .htaccess
file to apply rewrite rules, so first you need to allow changes to the file. Open the default Apache configuration file using nano
or your favorite text editor.
Inside that file, you will find a <VirtualHost *:80>
block starting on the first line. Inside of that block, add the following new block so your configuration file looks like the following. Make sure that all blocks are properly indented.
Save and close the file. To put these changes into effect, restart Apache.
Now, create the .htaccess
file in the web root.
Add this line at the top of the new file to activate the rewrite engine.
Save the file and exit.
You now have an operational .htaccess
file which you can use to govern your web application’s routing rules. In the next step, we will create sample website files that we’ll use to demonstrate rewrite rules.
Step 3 — Configuring URL Rewrites
Here, we will set up a basic URL rewrite, which converts pretty URLs into actual paths to code. Specifically, we will allow users to access http://your_server_ip/about
.
Begin by creating a file named about.html
in the web root.
Copy the following HTML code into the file, then save and close it.
You can access this page at http://your_server_ip/about.html
, but notice that if you try to access http://your_server_ip/about
, you will see a 404 Not Found error. If you would users to access the page using simply about
instead, rewrite rules will allow this very functionality.
All RewriteRules
abide by the following format:
RewriteRule
specifies the directive.pattern
is a regular expression that matches the desired string from the URL, which is what the viewer types in the browser.substitution
is the path to the actual URL, i.e. the path of the file Apache servers.flags
are optional parameters that can modify how the rule works.
Open up the .htaccess
file.
After the first line, add the RewriteRule
marked in red and save the file.
In this case, ^about$
is the pattern, about.html
is the substitution, and [NC]
is a flag. Our example uses a few characters with special meaning:
^
indicates the start of the URL, afteryour_server_ip/
.$
indicates the end of the URL.about
matches the string “about”.about.html
is the actual file that the user accesses.[NC]
is a flag that makes the rule case insensitive.
Now, you should be now able access http://your_server_ip/about
in your browser. In fact, with the rule shown above, the following URLs will point to about.html
:
http://your_server_ip/about
, because of the rule definition.http://your_server_ip/About
, because the rule is case insensitive.http://your_server_ip/about.html
, because original proper filename will always work.
The following will not:
http://your_server_ip/about/
, because the rule explicitly states that there may be nothing afterabout
using the$
character.http://your_server_ip/contact
, because it won’t match theabout
string in the rule.
You now have an operational .htaccess
file with a simple rule that you can modify and extend to your needs. In the following sections, we will show two additional examples of commonly used directives.
Example 1 — Simplifying Query Strings with RewriteRule
Web applications often make use of query strings, which are appended to a URL using a question mark (?
) after the address. Separate parameters are delimited using an ampersand (&
). Query strings may be used for passing additional data between individual application pages.
For example, a search result page written in PHP may use a URL like http://example.com/results.php?item=shirt&season=summer
. In this example, two additional parameters are passed to the imaginary result.php
application script: item
, with the value shirt
, and season
with the value summer
. The application may use the query string information to build the right page for the visitor.
Apache rewrite rules are often employed to simplify such long and unpleasent links as the above into friendly URLs that are easier to type and interpret visually. In this example, we would like to simplify the above link to become http://example.com/shirt/summer
. The shirt
and summer
parameter values are still in the address, but without the query string and script name.
Here’s one rule to implement this:
The shirt/summer
is explicitly matched in the requested address and Apache is told to serve results.php?item=shirt&season=summer
instead.
The [QSA]
flags are commonly used in rewrite rules. They tell Apache to append any additional query string to the served URL, so if the visitor types http://example.com/shirt/summer?page=2
the server will respond with results.php?item=shirt&season=summer&page=2
. Without it, the additional query string would get discarded.
While this method achieves the desired effect, both the item name and season are hardcoded into the rule. This means the rule will not work for any other items, like pants
, or seasons, like winter
.
To make the rule more generic, we can use regular expressions to match parts of the original address and use those parts in a substitution pattern. The modified rule will then look as follows:
The first regular expression group in parenthesis matches a string containing alphanumeric characters and numbers like shirt
or pants
and saves the matched fragment as the $1
variable. The second regular rexpression group in parenthesis matches exactly summer
, winter
, fall
, or spring
, and similarly saves the matched fragment as $2
.
The matched fragments are then used in the resulting URL in item
and season
variables instead of hardcoded shirt
and summer
values we used before.
The above will convert, for example, http://example.com/pants/summer
into http://example.com/results.php?item=pants&season=summer
. This example is also future proof, allowing mutliple items and seasons to be correctly rewritten using a single rule.
Example 2 — Adding Conditions with Logic Using RewriteConds
Rewrite rules are not necessarily always evaluated one by one without any limitations. The RewriteCond
directive lets us add conditions to our rewrite rules to control when the rules will be processed. All RewriteConds
abide by the following format:
RewriteCond
specifies theRewriteCond
directive.TestString
is the string to test against.Condition
is the pattern or condition to match.Flags
are optional parameters that may modify the condition and evaluation rules.
If a RewriteCond
evaluates to true, the RewriteRule
immediately following will be considered. If it won’t, the rule will be discarded. Multiple RewriteCond
may be used one after another and, with default behaviour, all must evaluate to true for the following rule to be considered.
As an example, let’s assume you would like to redirect all requests to non-existent files or directories on your site back to the home page instead of showing the standard 404 Not Found error page. This can be achieved with following conditions rules:
Apache2 Mod Rewrite
With the above:
%{REQUEST_FILENAME}
is the string to check. In this case, it’s the requested filename, which is a system variable available for every request.-f
is a built-in condition which verifies if the requested name exists on disk and is a file. The!
is a negation operator. Combined,!-f
evaluates to true only if a specified name does not exist or is not a file.- Similarly,
!-d
evaluates to true only if a specified name does not exist or is not a directory.
The RewriteRule
on the final line will come into effect only for requests to non-existent files or directories. The RewriteRule
itself is very simple and redirects every request to the /
website root.
mod_rewrite
is a useful Apache module that can be used effectively to ensure human-readable URLs. In this tutorial, you learned how to use the RewriteRule
directive to redirect URLs, including ones with query strings. You also learned how to conditionally redirect URLs using the RewriteCond
directive.
If you’d like to learn more about mod_rewrite
, take a look at Apache’s mod_rewrite Introduction and Apache’s official documentation for mod_rewrite.
by Ruslan Yakushev
Introduction
The URL Rewrite Module in IIS 7 and above provides an import feature that greatly simplifies the process of converting Apache mod_rewrite rules to IIS URL rewrite rules. In this walkthrough, you use the Import Rules feature provided in the URL Rewrite Module to import several mod_rewrite rules into an IIS configuration file. If you have not yet downloaded the URL Rewrite Module, you can do so at https://www.iis.net/downloads/microsoft/url-rewrite.
Set Up a Walkthrough Scenario
To see how you can convert mod_rewrite rules and verify that the converted rules work correctly, you will implement the common scenario of enforcing canonical host names for a Web site. In this example, you will force the use of www.mysite.com
instead of mysite.com
, so that when a request is made that uses a host name other than www.mysite.com
, you can redirect the request to a canonical hostname.
Start IIS Manager, and then click Default Web Site.
In the Actions pane, click on Bindings, and add a new http binding for port 8088.
Figure 1: Add a new binding
Using Notepad, open
%SystemDrive%windowssystem32driversetchosts
and add the two following lines at the end of the file:Notice that you are using '_' instead of '.' for domain separators. This is to prevent the Web browser from trying to resolve the domain name by using a Domain Name System (DNS) server.
Save the hosts file.
Verify that the host names were setup correctly by opening a Web browser and going to the sites
http://www_mysite_com/iisstart.htm
and tohttp://mysite_com/iisstart.htm
.
Convert mod_rewrite Rules
The Apache mod_rewrite rules to use for enforcing canonical host names are:
To convert these rules to IIS URL rewrite–specific format:
Start IIS Manager.
On the left, in the Connections pane, select Default Web Site.
On the right, in Features View, click URL Rewrite.
Figure 2: Click URL Rewrite
On the right, in the Actions pane, click Import Rules.
Copy the example mod_rewrite rules above and past them into the Rewrite rules text box.
Figure 3: Rules to import
The Tree View tab of the Converted Rules box instantly shows the result of the conversion. You can also click the XML View tab to see how the rules are stored in the Web.config file.
Figure 4: Converted rules
If you switch back to Tree View and select a node there, the corresponding mod_rewrite rule directive in the Rewrite rules text box will be highlighted.
Figure 5: Tree view
Note that during the rules conversion the rules were assigned default names. To change the default names to something more meaningful, select a rule in Tree View, right-click it, and select Rename from the context menu.
Figure 6: Rename
Change the name of the first rule from ImportedRule1 to Redirect to www_mysite_com:non-80. Change the name of the second rule from ImportedRule2 to Redirect to www_mysite_com:80.
In the Actions pane, click Apply to save the converted rules to the Web.config file, and then click Back to rules.
Figure 7: Back to Rules
Test the Converted Rules
To test that the rules imported from mod_rewrite format work correctly, open a Web browser, and go to either one of the following URLs:
http://localhost/iisstart.htm
http://mysite_com/iisstart.htm
In both cases, the Web browser is redirected to http://www_mysite_com/iisstart.htm
.
Also, if you try either of these URLs:
http://localhost:8088/iisstart.htm
http://mysite_com:8088/iisstart.htm
the Web browser gets redirected to http://www_mysite_com:8088/iisstart.htm
.
Apache Mod_rewrite Http To Https
Note that the rules that were imported from mod_rewrite enabled the enforcement of canonical host names for a Web site. A Bing search will reveal other examples of Apache mod_rewrite rules.
Disclaimer
Apache Mod_rewrite Http To Https Google
IMPORTANT - The URL Rewrite Module tries to convert Apache mod_rewrite rules to functionally equivalent IIS URL rewrite rules. However, not all mod_rewrite rules can be converted because of architectural differences between Apache and IIS. It is highly recommended that you study a mod_rewrite rule set until you understand its functionality before you begin the conversion process. Then, after converting to IIS URL rewrite rules, review and test the result of the conversion to make sure that the corresponding IIS rewrite rule set provides the same URL rewriting logic.
Note that only rules that follow Apache mod_rewrite syntax can be converted. Any other formats of rewrite rules (for example, ISAPI_Rewrite, Ionic ISAPI Rewrite, IISRewrite, and others) are not recognized or will be converted incorrectly.