{"id":8738,"date":"2016-01-23T16:34:37","date_gmt":"2016-01-23T16:34:37","guid":{"rendered":"https:\/\/wp.okra.host\/article\/redirects-with-mod_rewrite\/"},"modified":"2021-03-07T14:48:49","modified_gmt":"2021-03-07T13:48:49","slug":"redirects-with-mod_rewrite","status":"publish","type":"ht_kb","link":"https:\/\/kb.okra.host\/de\/article\/redirects-with-mod_rewrite\/","title":{"rendered":"Umleitungen mit mod_rewrite"},"content":{"rendered":"<h2 id=\"overview\" >Overview<\/h2>\n<p>A redirect changes the URL, in browser, from one URL to another. A variety of redirect codes exist to force a variety of behaviors in the browser (or spider).<\/p>\n<h2 id=\"usage\" >Usage<\/h2>\n<p>All redirects are controlled through a <a href=\"https:\/\/kb.okra.host\/guides\/htaccess-guide\/\">.htaccess<\/a> file in the <a href=\"https:\/\/kb.okra.host\/web-content\/where-is-site-content-served-from\/\">document root<\/a> of your target <a href=\"https:\/\/kb.okra.host\/control-panel\/creating-addon-domain\/\">domain<\/a> or <a href=\"https:\/\/kb.okra.host\/web-content\/creating-subdomain\/\">subdomain<\/a>. The following stanza is a common, and simple redirect if the URL is www.example.com, then redirect to example.com:<\/p>\n<pre>RewriteEngine On\r\nRewriteBase \/\r\nRewriteCond %{HTTP_HOST} ^www.example.com$\r\nRewriteRule ^(.*)$ http:\/\/example.com\/$1 [R,L]<\/pre>\n<p>Likewise, a converse rule to redirect example.com to www.example.com would be a minor alteration of <code>RewriteCond<\/code> and <code>RewriteRule<\/code>:<\/p>\n<pre>RewriteEngine On\r\nRewriteBase \/\r\nRewriteCond %{HTTP_HOST} ^example.com$\r\nRewriteRule ^(.*)$ http:\/\/www.example.com\/$1 [R,L]<\/pre>\n<p>Notice the difference? RewriteCond stands for &#8220;rewrite condition&#8221; and RewriteRule &#8220;rewrite rule&#8221;. That is to say, if this <em>condition<\/em> matches, apply this <em>rule<\/em>, which consists of a regular expression substitution, to the current URL. Rewrites operate using\u00a0<a href=\"http:\/\/httpd.apache.org\/docs\/current\/rewrite\/intro.html\">regular expressions<\/a>, in particular <a href=\"https:\/\/en.wikipedia.org\/wiki\/Perl_Compatible_Regular_Expressions\">Perl-compatible (&#8220;PCRE&#8221;)<\/a>.<\/p>\n<h2 id=\"required-components\" >Required components<\/h2>\n<p>A redirect <em><strong>always<\/strong><\/em> requires two crucial components:<\/p>\n<pre>RewriteEngine On\r\nRewriteBase \/<\/pre>\n<p><code>RewriteEngine On<\/code> enables URL rewriting. <code>RewriteBase \/<\/code> anchors all rules to the current directory. [su_highlight]RewriteBase is absolutely necessary when used with addon domains and subdomains.[\/su_highlight]<\/p>\n<p>The following examples will omit these 2 lines for brevity:<\/p>\n<h3 id=\"examples\" >Examples<\/h3>\n<h4 id=\"redirect-to-ssl\" >Redirect to SSL<\/h4>\n<pre>RewriteCond %{HTTPS} !^on$\r\nRewriteRule ^(.*)$ https:\/\/%{HTTP_HOST}\/$1 [R,L]<\/pre>\n<p><span style=\"color: #3366ff\"><strong>Discussion:<\/strong><\/span>\u00a0look for an environment variable called <code>HTTPS<\/code>. Whenever SSL is used, this variable is set by the web server. If no flag is set, assume normal HTTP traffic. Redirect to SSL (https) and keep the hostname (<code>%{HTTP_HOST}<\/code>) + URL path (<code>$1<\/code>, captured from left-side)<\/p>\n<h3 id=\"redirect-codes\" >Redirect codes<\/h3>\n<p>An optional number may be added onto a R flag to specify a\u00a0redirect code. By default a 302 is sent in a redirect response. The following redirect codes may be used by changing <code>R<\/code> to <code>R=XXX<\/code>, where XXX is one of the following codes:<\/p>\n<p>[su_table]<\/p>\n<table>\n<thead>\n<tr>\n<th><strong>Code<\/strong><\/th>\n<th><strong>Name<\/strong><\/th>\n<th><strong>Description<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>301<\/td>\n<td>Moved Permanently<\/td>\n<td>Resource is no longer accessible under URL, use new URL<\/td>\n<\/tr>\n<tr>\n<td>302<\/td>\n<td>Found<\/td>\n<td>URL resides temporarily under new resource, continue to use this URL<\/td>\n<\/tr>\n<tr>\n<td>303<\/td>\n<td>See Other<\/td>\n<td>Response resides elsewhere and should be retrieved using a GET; used mostly with POST requests<\/td>\n<\/tr>\n<tr>\n<td>307<\/td>\n<td>Temporary Redirect<\/td>\n<td>URL resides elsewhere; new location may differ on different requests. Continue to use this URL in future.<\/td>\n<\/tr>\n<tr>\n<td>308<\/td>\n<td>Permanent Redirect<\/td>\n<td>Similar to 301, works with POST, but <em><a href=\"https:\/\/tools.ietf.org\/html\/rfc7238\">experimental<\/a><\/em><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>[\/su_table]<\/p>\n<h2 id=\"see-also\" >See also<\/h2>\n<ul>\n<li><a href=\"http:\/\/httpd.apache.org\/docs\/current\/rewrite\/intro.html\">Introduction to regular expressions and mod_rewrite<\/a> (httpd.apache.org)<\/li>\n<li><a href=\"http:\/\/httpd.apache.org\/docs\/current\/rewrite\/\">URL rewriting with mod_rewrite<\/a> (httpd.apache.org)<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Overview A redirect changes the URL, in browser, from one URL to another. A variety of redirect codes exist to force a variety of behaviors in the browser (or spider). Usage All redirects are controlled through a .htaccess file in the document root of your target domain or subdomain. The&#8230;<\/p>","protected":false},"author":1,"comment_status":"open","ping_status":"closed","template":"","format":"standard","meta":{"footnotes":""},"ht-kb-category":[54],"ht-kb-tag":[],"class_list":["post-8738","ht_kb","type-ht_kb","status-publish","format-standard","hentry","ht_kb_category-web-content"],"_links":{"self":[{"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb\/8738","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb"}],"about":[{"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/comments?post=8738"}],"version-history":[{"count":1,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb\/8738\/revisions"}],"predecessor-version":[{"id":8739,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb\/8738\/revisions\/8739"}],"wp:attachment":[{"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/media?parent=8738"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb-category?post=8738"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb-tag?post=8738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}