: What are the most commonly used and basic Apache htaccess redirects? This question is here so we can offer users who are looking for information on how to make one or more common or basic
This question is here so we can offer users who are looking for information on how to make one or more common or basic redirects in Apache using the htaccess file. All future questions pertaining to finding information that is that is covered by the question should be closed as a duplication of this question. As per this Meta question.
Whats the point in this question? The idea while not perfect is catch the most commonly asked questions regarding redirects using the htaccess on the Apache platform either on some type of lamp or a live server. The type of answers should be generally those that you could imagine are used by 100,000’s of sites world-wide and are constantly asked here at Pro Webmasters repeatedly over and over in various forms.
A few examples of the type of answers we are looking for:
How can I redirect non-www to www?
How can I redirect a sub domain to the main domain?
How can I redirect a sub folder from domain to a root or a subdomain?
How can I redirect an old URL to a new URL?
A few examples of the types of answers that we are not looking for:
Answers that do not involve a redirect.
Any answers relating to NGinx, IIS or any other non-Apache platform.
Answers that involve custom and complex string or query removals.
Resources for Advanced to Complex Mod_Rewrite Rules:
Everything you ever wanted to know about mod rewrite rules but were afraid to ask
Please note: that this question is still in construction and may need some refining either by myself or a real moderator of Pro Webmasters, if you have any concerns or questions please use the meta page I made a few days back here.
More posts by @Turnbaugh106
1 Comments
Sorted by latest first Latest Oldest Best
Mod_alias and Mod_rewrite
There are 2 modules you can use within the htaccess for controlling redirects these are known as mod_alias and mod_rewrite. To help you establish the difference between the two below is some commands to familiarize when your using mod_alias or mod_rewrite.
mod_alias commands
Alias
AliasMatch
Redirect
RedirectMatch
RedirectPermanent
RedirectTemp
ScriptAlias
ScriptAliasMatch
mod_rewrite commands
RewriteBase
RewriteCond
RewriteEngine
RewriteMap
RewriteOptions
RewriteRule
mod_alias is considered a simpler redirect method, while mod_rewrite is considered more advanced and allows more directive. It's worth noting that mod_rewrite commands are exercuted before mod_alias.
Redirect Codes
301 Permanent
302 Temporary
How to redirect a old URL to new URL?
There are many ways that you can redirect one page to another using the htaccess, 301 is generally the most important and used since it most often pages will need permanent redirects and not temporary. Permanent Redirects are great for attempting to keep rankings on one page to another.
301 Permanents
Redirect 301: Redirect 301 /old.html www.example.com/new-url/
RedirectPermanent: RedirectPerm /old.html www.example.com/new-url/
RedirectMatch: RedirectMatch 301 ^old.html$ www.example.com/new-url/
RewriteRule:
RewriteEngine On
RewriteBase /
RewriteRule ^old.html$ www.example.com/new-url/ [R=301,NC,L]
302 Temporary's
Redirect 302: Redirect 302 /old.html www.example.com/new-url/
RedirectTemp : RedirectTemp /old.html www.example.com/new-url/
RedirectMatch: RedirectMatch 302 ^old.html$ www.example.com/new-url/
RewriteRule:
RewriteEngine On
RewriteBase /
RewriteRule ^old.html$ www.example.com/new-url/ [R,NC,L]
Catch all and redirect non-www to www
You should opt to use mod_write for redirecting all requests for non www versions of your site because the varible will catch page names, so example.com/page1/ will automaticly redirect to example.com/page1/.
mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ www.%{HTTP_HOST}/ [R=301,L]
mod_alias:
<If "%{HTTP_HOST} != 'www.example.com'">
Redirect permanent / www.example.com/ </If>
Catch all and redirect www to no-www
mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ example.com [R=301,L]
mod_alias:
<If "%{HTTP_HOST} != 'example.com'">
Redirect permanent / example.com/ </If>
Catch all and redirect www to no-www (HTTPS)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ example.com [R=301,L]
Enforce SSL on specific pages and disable on rest
This script will remove SSL on all other pages part from the login page and register page, you can add more just use | as the separator between file names.
mod_rewrite:
RewriteCond %{HTTPS} on
RewriteCond %{SCRIPT_FILENAME} !/(login|register).php [NC]
RewriteRule ^(.*)$ %{HTTP_HOST}/ [R=301,L]
Enforce SSL on the entire site
If you want to enforce SSL on the complete site then you can use mod_rewrite to detect HTTPS off.
mod_rewrite:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) %{HTTP_HOST}%{REQUEST_URI}
Removing extensions from URL using mod_rewrite
Some web hosts may require Options -MultiViews in your htaccess to remove these extensions, test it without and if it doesn't work simply add it.
Removing extension type PHP:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ .php [NC,L]
Removing extension type HTML:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ .html [NC,L]
Removing extension type HTM:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ .htm [NC,L]
Not Working? Try this (change php to the file extension you want to remove):
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ .php [NC,L]
Removing and Enforce tailing slashes
Enforcing:
This will redirect all requests without a tailing / to the URL with the slash on the end. (note within the 2nd part of the bracket is those file extensions to ignore.. Since it wouldn't make sense to enforce / on a picture URL.
RewriteCond %{REQUEST_URI} !.(php|html?|jpg|gif)$
RewriteRule ^(.*)([^/])$ %{HTTP_HOST}// [L,R=301]
Removing:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (.*)$
RewriteRule ^(.+)/$ www.example.com/ [R=301,L
Convert backslashes into forward slashes
This will convert all backslashes found in a URL to forward slashes. Good for preventing duplicate content:
RewriteEngine On
RewriteCond %{REQUEST_URI} (.*)(.*)
RewriteRule .* %1/%2 [R=301]
Redirect Old Domain to New Domain
RewriteEngine On
# Redirect Entire Site to New Domain
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^another.olddomain.com$ [NC]
RewriteRule ^(.*)$ newdomain.com/ [R=301,L]
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.