{"id":1087,"date":"2009-11-18T11:47:33","date_gmt":"2009-11-18T09:47:33","guid":{"rendered":"http:\/\/www.blog.atbliss.ru\/?p=1087"},"modified":"2017-10-13T00:28:32","modified_gmt":"2017-10-12T22:28:32","slug":"mod-rewrite","status":"publish","type":"post","link":"https:\/\/atbliss.ru\/mod-rewrite\/","title":{"rendered":"\u0411\u043e\u043b\u0435\u0435 10 \u043f\u0440\u0430\u0432\u0438\u043b mod_rewrite, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0437\u043d\u0430\u0442\u044c"},"content":{"rendered":"
\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435<\/p>
Mod_rewrite is an Apache-based rewrite engine for dynamically rewriting URLs. It\u2019s built into Apache servers natively, though not enabled by default.<\/p>\n
It\u2019s capable of functions beyond simple rewrites, though, some of which are included below.<\/p>\n
Mod_rewrite is used through your .htaccess file. Place the following code at the beginning of your .htaccess file to turn mod_rewrite on:<\/p>\n
RewriteEngine on<\/pre>\n(Don\u2019t forget that .htaccess commands are case-sensitive.) This code needs to be entered at the beginning of any .htaccess file using mod_rewrite.<\/p>\n
The Basic Mod_Rewrite Layout<\/span><\/h3>\n
The basic format for a mod_rewrite command is:<\/p>\n
\n
- RewriteRule\u00a0Pattern\u00a0Substitution\u00a0[Flag(s)]<\/li>\n<\/ol>\n
RewriteRule Pattern Substitution [Flag(s)]<\/pre>\nURLs are Always Relative<\/span><\/h3>\n
The URL you redirect to is always relative to the directory in which your .htaccess file is placed. So if it\u2019s in the root directory, URLs are all in relation to the root directory; if it\u2019s in a sudirectory, URLs are in relation to that particular subdirectory.<\/p>\n
A Basic Redirect<\/span><\/h3>\n
If you just want to create a simple 301 redirect from one URL to another, then use the following code:<\/p>\n
\n
- RewriteRule\u00a0^fileone.html$\u00a0filetwo.html<\/li>\n<\/ol>\n
RewriteRule ^fileone.html$ filetwo.html<\/pre>\nThis is a very basic rule that means any requests for fileone.html will be sent to filetwo.html.<\/p>\n
Require no \u201cwww\u201d<\/span><\/h3>\n
This bit of code will make it so visitors to your site don\u2019t need to type in the \u201cwww\u201d bit of your website address.<\/p>\n
\n
- RewriteCond\u00a0%{HTTP_HOST}\u00a0!^domain.com$\u00a0[NC]<\/li>\n
- RewriteRule\u00a0^(.*)$\u00a0http:\/\/domain.com\/$1\u00a0[R=301,L] <\/span><\/li>\n<\/ol>\n
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]\r\nRewriteRule ^(.*)$ \/\/domain.com\/$1 [R=301,L]<\/pre>\nBlock a Specific IP Address<\/span><\/h3>\n
If you want to block someone coming from a specific IP address<\/a>\u00a0from accessing your website, you can use the following code:<\/p>\n
\n
- RewriteCond\u00a0%{REMOTE_ADDR}\u00a0^(A.B.C.D)$<\/li>\n
- RewriteRule\u00a0^\/*\u00a0http:\/\/www.domain.com\/sorry.html\u00a0[L] <\/span><\/li>\n<\/ol>\n
RewriteCond %{REMOTE_ADDR} ^(A.B.C.D)$\r\nRewriteRule ^\/* \/\/www.domain.com\/sorry.html [L]<\/pre>\nReplace the A.B.C.D with the IP address you want to block (don\u2019t forget to leave the \u201c\u201d before each dot, which escapes the character).<\/p>\n
Block Specific User Agents<\/span><\/h3>\n
If you want to block a group of IP addresses using the same User Agent (bot), the following code with do it:<\/p>\n
\n
- RewriteCond\u00a0%{HTTP_USER_AGENT}\u00a0UserAgent<\/li>\n
- RewriteRule\u00a0.*\u00a0—\u00a0[F,L]<\/li>\n<\/ol>\n
RewriteCond %{HTTP_USER_AGENT} UserAgent\r\nRewriteRule .* - [F,L]<\/pre>\nJust replace the \u201cUserAgent\u201d bit with whatever user agent you want to block. You can also block more than one at a time by replacing the top line in that code with something like this:<\/p>\n
\n
- RewriteCond\u00a0%{HTTP_USER_AGENT}\u00a0UserAgentA\u00a0[OR]<\/li>\n
- RewriteCond\u00a0%{HTTP_USER_AGENT}\u00a0UserAgentB<\/li>\n<\/ol>\n
RewriteCond %{HTTP_USER_AGENT} UserAgentA [OR]\r\nRewriteCond %{HTTP_USER_AGENT} UserAgentB<\/pre>\nYou can put as many user agents in as you want, just make sure you end each line with [OR] (with the exception of the last line, of course).<\/p>\n
Strip Query Strings<\/span><\/h3>\n
Let\u2019s say all the pages on your site other than your home page are formatted as follows, with query strings instead of page names:<\/p>\n
\/\/www.domain.com\/home.html?example=12345abcd<\/p>\n
Those aren\u2019t very pretty, and on top of that, search engines will show a bunch of duplicated \u201chome\u201d pages. If you want to get rid of the query string in your page URLs, use the following code:<\/p>\n
\n
- RewriteCond\u00a0%{QUERY_STRING}\u00a0example=<\/li>\n
- RewriteRule\u00a0(.*)\u00a0http:\/\/www.domain.com\/$1?\u00a0[R=301] <\/span><\/li>\n<\/ol>\n
RewriteCond %{QUERY_STRING} example=\r\nRewriteRule (.*) \/\/www.domain.com\/$1? [R=301]<\/pre>\nThis not only gets rid of the query string, but also the preceding question mark.<\/p>\n
Set up a Default Image<\/span><\/h3>\n
Using a default, backup image in case of broken images can make your site look more professional. Use the following code to redirect to a default image for any image whose file cannot be found.<\/p>\n
\n
- RewriteCond\u00a0%{REQUEST_FILENAME}\u00a0!-f<\/li>\n
- RewriteRule\u00a0^images\/.*.jpg$\u00a0\/images\/default.jpg\u00a0[L] <\/span><\/li>\n<\/ol>\n
RewriteCond %{REQUEST_FILENAME} !-f\r\nRewriteRule ^images\/.*.jpg$ \/images\/default.jpg [L]<\/pre>\nOf course, you can change the \u201c.jpg\u201d bit to whatever file type you\u2019re using. Make sure you have an image called \u201cdefault.jpg\u201d or change that to whatever your default image filename is.<\/p>\n
Prevent Hotlinking<\/span><\/h3>\n
The last thing most website owners want is other sites stealing their content or worse\u2014hotlinking to their images and stealing their bandwidth. Here\u2019s a simple bit of code that prevents it:<\/p>\n
\n
- RewriteCond\u00a0%{HTTP_REFERER}\u00a0!^$<\/li>\n
- RewriteCond\u00a0%{HTTP_REFERER}\u00a0!^http:\/\/(www.)?domain.com\/\u00a0.*$\u00a0[NC] <\/span><\/li>\n
- RewriteRule\u00a0.(gif|jpg|swf|flv|png)$\u00a0\/feed\/\u00a0[R=302,L]<\/li>\n<\/ol>\n
RewriteCond %{HTTP_REFERER} !^$\r\nRewriteCond %{HTTP_REFERER} !^\/\/(www.)?domain.com\/ .*$ [NC]\r\nRewriteRule .(gif|jpg|swf|flv|png)$ \/feed\/ [R=302,L]<\/pre>\nMake sure you change the \u201cdomain.com\u201d bit to your own domain name.<\/p>\n
Redirect to a Maintenance Page<\/span><\/h3>\n
If you need to take your entire site offline for a bit and redirect to a maintenance page (or some other page), use the following code:<\/p>\n
\n
- RedirectMatch\u00a0302\u00a0^\/\u00a0\/maintenancepage.html<\/li>\n<\/ol>\n
RedirectMatch 302 ^\/ \/maintenancepage.html<\/pre>\nChange the \u201cmaintenancepage.html\u201d bit to wherever your maintenance page file is located.<\/p>\n
Redirect Multiple Domains to a Single Domain<\/span><\/h3>\n
If you have multiple domains pointing to your site, it\u2019s possible you could take a hit in the search engines for having duplicate content. Use the following code to redirect visitors from two domains to just one:<\/p>\n
\n
- RewriteCond\u00a0%{HTTP_HOST}\u00a0^www.domain.net$\u00a0[NC,OR]<\/li>\n
- RewriteCond\u00a0%{HTTP_HOST}\u00a0^domain.net$\u00a0[NC,OR]<\/li>\n
- RewriteCond\u00a0%{HTTP_HOST}\u00a0^www.domain.net$\u00a0[NC]<\/li>\n
- RewriteRule\u00a0^(.*)$\u00a0http:\/\/domain.net\/$1\u00a0[R=301,L] <\/span><\/li>\n<\/ol>\n
RewriteCond %{HTTP_HOST} ^www.domain.net$ [NC,OR]\r\nRewriteCond %{HTTP_HOST} ^domain.net$ [NC,OR]\r\nRewriteCond %{HTTP_HOST} ^www.domain.net$ [NC]\r\nRewriteRule ^(.*)$ \/\/domain.net\/$1 [R=301,L]<\/pre>\nRemember the Filesystem Always Takes Precedence<\/span><\/h3>\n
The filesystem on your server will always take precedence over the rewritten URL. For example, if you have a directory named \u201cservices\u201d and within that directory is a file called \u201cdesign.html\u201d, you can\u2019t have the URL redirect to \u201c\/\/domain.com\/services\u201d. What happens is that Apache goes into the \u201cservices\u201d directory and doesn\u2019t see the rewrite instructions.<\/p>\n
To fix this, simply rename your directory (adding an underscore to the beginning or end is a simple way to do that).<\/p>\n
Remember:<\/span><\/h3>\n
\n
- Because mod_rewrite works within the .htaccess file, commands are case sensitive.<\/li>\n
- Always back up your .htaccess file before making any changes to it. This way, if there\u2019s a problem, you can easily restore your site.<\/li>\n<\/ul>\n
More Resources:<\/span><\/h3>\n
\n
- Modrewrite.com<\/span> \u2013 This is a great site that offers a forum, a beginner\u2019s guide, and links to more information about mod_rewrite.<\/li>\n
- Mod_Rewrite Tips and Tricks<\/a> \u2013 A great article on the basics of mod_rewrite and some beginner and advanced techniques.<\/li>\n
- Mod Rewrite Tips and Tricks<\/a> \u2013 This site offers a few basic tips for mod_rewrite.<\/li>\n
- Apache Mod_Rewrite Cheat Sheet<\/a> \u2013 This cheat sheet offers information on the most-used items for building a mod_rewrite pattern.<\/li>\n
- Learn Apache mod_rewrite: 13 Real-World Examples<\/a> \u2013 This is a much more advanced article from SitePoint.<\/li>\n
- Module mod_rewrite URL Rewriting Engine<\/a> \u2013 The official documentation from Apache.<\/li>\n
- .htaccess Rewrite Tips for Using RewriteRule and RewriteCond for .htaccess mod_rewrite<\/a> \u2013 A great guide to a number of different mod_rewrite patterns.<\/li>\n
- Tip for Configuring Apache\u2019s Mod_Rewrite<\/a> \u2013 Five great tips for mod_rewrite configuration.<\/li>\n
- Several Mod_Rewrite Tricks for a Better Web Application<\/a> \u2013 Covers a number of basic and more advanced mod_rewrite tricks.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"
\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435Turn Mod_Rewrite OnThe Basic Mod_Rewrite LayoutURLs are Always RelativeA Basic RedirectRequire no \u201cwww\u201dBlock a Specific IP AddressBlock Specific User AgentsStrip Query StringsSet up a Default ImagePrevent HotlinkingRedirect to a Maintenance PageRedirect Multiple Domains to a Single DomainRemember the Filesystem Always Takes PrecedenceRemember:More Resources: Mod_rewrite is an Apache-based rewrite engine for dynamically rewriting URLs. It\u2019s built […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[695],"tags":[],"yoast_head":"\n
\u0411\u043e\u043b\u0435\u0435 10 \u043f\u0440\u0430\u0432\u0438\u043b mod_rewrite, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0437\u043d\u0430\u0442\u044c<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n