{"id":1119,"date":"2010-01-11T21:52:01","date_gmt":"2010-01-12T02:52:01","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=1119"},"modified":"2014-12-18T17:50:38","modified_gmt":"2014-12-18T22:50:38","slug":"apache-2-mod_rewrite","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=1119","title":{"rendered":"Apache 2 mod_rewrite"},"content":{"rendered":"<div class='toc wptoc'>\n<h2>Contents<\/h2>\n<ol class='toc-odd level-1'>\n\t<li>\n\t\t<a href=\"#Overview\">Overview<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Enable_mod_rewrite\">Enable mod_rewrite<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#mod_rewrite_Comments\">mod_rewrite Comments<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Logging\">Logging<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#mod_rewrite_Flags\">mod_rewrite Flags<\/a>\n\t\t\t<\/li>\n\t\t<\/ol>\n\t<li>\n\t\t<a href=\"#Setup_URL_Rewrite\">Setup URL Rewrite<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Use_httpd.conf\">Use httpd.conf<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Use_.htaccess\">Use .htaccess<\/a>\n\t\t\t<\/li>\n\t\t<\/ol>\n\t<li>\n\t\t<a href=\"#Modify_Query_String\">Modify Query String<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#References\">References<\/a>\n\t<\/li>\n<\/ol>\n<\/ol>\n<\/div>\n<div class='wptoc-end'>&nbsp;<\/div>\n<span id=\"Overview\"><h2>Overview<\/h2><\/span>\n<span id=\"Enable_mod_rewrite\"><h3>Enable mod_rewrite<\/h3><\/span>\n<p>* Edit httpd.conf and uncomment this line:<\/p>\n<pre lang=\"txt\">\r\nLoadModule rewrite_module modules\/mod_rewrite.so\r\n<\/pre>\n<span id=\"mod_rewrite_Comments\"><h3>mod_rewrite Comments<\/h3><\/span>\n<p>* You can use RewriteEngine off to ignore rewrite statements<\/p>\n<pre lang=\"xml\">\r\nRewriteEngine off  \r\n\r\nRewriteCond %{HTTP_HOST} !^www.example.com$ [NC]  \r\n\r\nRewriteRule .? http:\/\/www.example.com%{REQUEST_URI} [R=301,L]  \r\n\r\nRewriteEngine on\r\n<\/pre>\n<span id=\"Logging\"><h3>Logging<\/h3><\/span>\n<p>* Using <a href=\"http:\/\/httpd.apache.org\/docs\/current\/mod\/core.html#loglevel\">LogLevel<\/a> directive<br \/>\n* Apache 2.2 and below:<\/p>\n<pre lang=\"xml\">\r\nRewriteLog \/var\/log\/httpd\/rewrite.log\r\nRewriteLogLevel 9\r\n<\/pre>\n<p>* Apache 2.4 and above:<\/p>\n<pre lang=\"xml\">\r\n# most verbose is trace8\r\nLogLevel alert rewrite:trace3\r\n<\/pre>\n<span id=\"mod_rewrite_Flags\"><h3>mod_rewrite Flags<\/h3><\/span>\n<p>* last|L: terminate rewrite conditions\/rules<br \/>\n* nocase|NC: ignore case<br \/>\n* redirect|R: default to HTTP302 (redirected temporarily). Use [R=301] for permanent redirect<br \/>\n* qsappend|QSA: pass through existing query strings<br \/>\n* forbidden|F: Apache will issue 403 response<br \/>\n* ornext|OR: logical OR condition. Default is AND condition<br \/>\n* next|N: restart rewrite process from the first rule<\/p>\n<span id=\"Setup_URL_Rewrite\"><h2>Setup URL Rewrite<\/h2><\/span>\n<span id=\"Use_httpd.conf\"><h3>Use httpd.conf<\/h3><\/span>\n<p>* Create a test config file: <em>\/etc\/httpd\/conf.d\/testmodrewrite.conf<\/em> with the following content to redirect all request starting with test to google.com:<\/p>\n<pre lang=\"txt\">\r\n<IfModule rewrite_module>\r\nRewriteEngine On\r\nRewriteLog \/var\/log\/httpd\/rewrite.log\r\nRewriteLogLevel 9\r\n \r\n# Redirect any request starting with test to google.com\r\nRewriteRule ^\/test(.*) http:\/\/www.google.com\/ [P]\r\n<\/IfModule> \r\n<\/pre>\n<p>* Point your browser to <em>test.html<\/em> and you&#8217;ll be redirected to <em>google.com<\/em><\/p>\n<span id=\"Use_.htaccess\"><h3>Use .htaccess<\/h3><\/span>\n<p>* Allow <em>.htaccess<\/em> usage in <em>httpd.conf<\/em> file:<\/p>\n<pre lang=\"xml\">\r\n<Directory \"\/var\/www\/html\">\r\n    # Allow .htaccess\r\n    AllowOverride All\r\n<\/pre>\n<p>* Create <em>.htaccess<\/em> file in the test directory, e.g. <em>\/var\/www\/html<\/em><\/p>\n<pre lang=\"xml\">\r\nRewriteEngine on\r\n\r\nRewriteRule ^\/?test.html$ http:\/\/www.google.com [L]\r\n<\/pre>\n<p>* Point your browser to <em>test.html<\/em> and you&#8217;ll be redirected to <em>google.com<\/em><\/p>\n<span id=\"Modify_Query_String\"><h2>Modify Query String<\/h2><\/span>\n<p>* <a href=\"https:\/\/wiki.apache.org\/httpd\/RewriteQueryString\">Manipulating the Query String<\/a><\/p>\n<pre lang=\"bash\">\r\nRewriteCond %{QUERY_STRING} (.*(?:^|&))key=val((?:&|$).*)\r\nRewriteRule \/test(.*) \/test.html?%1other_val%2\r\n\r\nRewriteCond %{QUERY_STRING} ^key=(.*)$ [NC]\r\nRewriteRule ^\/test(.*) \/test.html?key=%1\r\n\r\nRewriteCond %{REQUEST_URI} ^\/test1 [NC]\r\nRewriteCond %{QUERY_STRING} ^key=(.*)$ [NC]\r\nRewriteRule ^\/test(.*) \/test.html?key=%1\r\n<\/pre>\n<span id=\"References\"><h2>References<\/h2><\/span>\n<p>* <a href=\"http:\/\/www.sitepoint.com\/apache-mod_rewrite-examples\/\">http:\/\/www.sitepoint.com\/apache-mod_rewrite-examples\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview Enable mod_rewrite * Edit httpd.conf and uncomment this line: LoadModule rewrite_module modules\/mod_rewrite.so mod_rewrite Comments * You can use RewriteEngine off to ignore rewrite statements RewriteEngine off RewriteCond %{HTTP_HOST} !^www.example.com$ [NC] RewriteRule .? http:\/\/www.example.com%{REQUEST_URI} [R=301,L] RewriteEngine on Logging * Using &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=1119\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[21],"tags":[],"class_list":["post-1119","post","type-post","status-publish","format-standard","hentry","category-apache"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-i3","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1119"}],"version-history":[{"count":8,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1119\/revisions"}],"predecessor-version":[{"id":10524,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1119\/revisions\/10524"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}