Difference between "Redirect permanent" vs. mod_rewrite RewriteRule

8,576

Solution 1

By default mod_rewrite does "302 Found" redirects, which are temporary. Assuming everything else is equal, Redirect permanent is equivalent to RewriteRule <blah> [R=permanent].

Solution 2

Is one better then the other?

Apache now recommends the use of Redirect from mod_alias instead of using RewriteRule from mod_rewrite. See https://httpd.apache.org/docs/current/rewrite/avoid.html#redirect , which says:

mod_rewrite should be considered a last resort, when other alternatives are found wanting. Using it when there are simpler alternatives leads to configurations which are confusing, fragile, and hard to maintain.

Solution 3

Search engines will see a permanent redirect and update their indexes accordingly.

Share:
8,576

Related videos on Youtube

Stefan Lasiewski
Author by

Stefan Lasiewski

Stefan Lasiewski Daddy, Linux Guy, Bicyclist, Tinkerer, Fixer &amp; Breaker of things. I work as a Senior SYstem Engineer at the National Energy Research Scientific Computing Center (NERSC) Division at Lawrence Berkeley National Laboratory (LBNL) in Berkeley, CA. Father of 3 cute children. Yes I'm a sysadmin and a parent. Heavy user of CentOS, RHEL &amp; FreeBSD for production services at work. I also run Ubuntu at home, for the simplicity. I'm a fan of Apache HTTP Server, Nagios &amp; Cacti. Original proposer of unix.stackexchange.com (Yes, this proposal predated askubuntu.com, and I wish they would have merged with the Unix proposal.).

Updated on September 18, 2022

Comments

  • Stefan Lasiewski
    Stefan Lasiewski almost 2 years

    This is an Apache httpd 2.2 server.

    We require that access to this webserver be encrypted by HTTPS.

    When web clients visit my site at http://www.example.org/$foo (port 80), I want to redirect their request to the HTTPS encrypted website at https://www.example.org/$foo .

    There seem to be two common ways to do this:

    First method uses the 'Redirect' directive from mod_alias:

    <VirtualHost *:80>
        Redirect permanent / https://www.example.org/
    </VirtualHost>
    

    Second method uses mod_rewrite:

    <VirtualHost *:80>
        RewriteEngine On
        RewriteCond %{HTTPS} off
        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
    </VirtualHost>
    

    What is the difference between a "Redirect permanent" and the mod_rewrite stanza. Is one better then the other?

  • Stefan Lasiewski
    Stefan Lasiewski over 8 years
    > Is one better then the other?
  • Gondy
    Gondy over 5 years
    Actually no, Redirect directive handles redirect from http to https better. See Stefan's comment below and check this section of Apache documentation: httpd.apache.org/docs/current/rewrite/avoid.html#redirect