{"id":195,"date":"2012-10-10T07:18:22","date_gmt":"2012-10-10T06:18:22","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=195"},"modified":"2012-10-10T07:18:22","modified_gmt":"2012-10-10T06:18:22","slug":"how-to-subdomains-on-apache-with-mod_rewrite","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=195","title":{"rendered":"How to subdomains on Apache with mod_rewrite"},"content":{"rendered":"<p>Because I like to play with a lot of projects and code, I create subdomains for oprsteny.cz for almost every project. Instead of going through the hassle of creating a CNAME or A record for every subdomain, creating an Apache vhost file and enabling it and creating the necessary directory structure, I figured there\u2019s an easier way to do so.<!--more--><\/p>\n<p>First thing I had to do was to create an *.oprsteny.cz wildcard record for my domain. This way, I don\u2019t need to create a DNS record every time I need a new subdomain. When this is done, I checked for the record for example using\u00a0<code>dig<\/code>, and I saw something like this:<\/p>\n<div>\n<div>\n<pre>$ dig *.oprsteny.cz\r\n# .. skipping some output\r\n;; ANSWER SECTION:\r\n*.oprsteny.cz.          3600    IN      A       192.0.32.10<\/pre>\n<\/div>\n<\/div>\n<p>Once that\u2019s done and it\u2019s pointing to my server, I need to create an\u00a0<a href=\"http:\/\/apache.org\/\">Apache<\/a>\u00a0vhost which handles the requests with\u00a0<a href=\"http:\/\/httpd.apache.org\/docs\/2.0\/mod\/mod_rewrite.html\">mod_rewrite<\/a>, which is a rules-based url rewriting module for apache, and can easily handle different subdomains and actions based on the subdomain.<\/p>\n<p>I was replacing the domain name, IP address and paths with my own, and I had a directory structure like this:<\/p>\n<div>\n<div>\n<pre>\/var\/www\/oprsteny.cz\/\r\n\/var\/www\/oprsteny.cz\/www\/\r\n\/var\/www\/oprsteny.cz\/subdomains\/\r\n\/var\/www\/oprsteny.cz\/subdomains\/&lt;any subdomain&gt;\/<\/pre>\n<\/div>\n<\/div>\n<p>Apache vhost (stripped version):<\/p>\n<div>\n<div>\n<pre>&lt;VirtualHost *:80&gt;\r\n        ServerName oprsteny.cz\r\n        ServerAlias *.oprsteny.cz\r\n        DocumentRoot \"\/var\/www\/oprsteny.cz\"\r\n\r\n        RewriteEngine on\r\n        # Rewrite everything for oprsteny.cz to the\r\n        # \/www\/ subdirectory\r\n        RewriteCond %{HTTP_HOST} ^oprsteny\\.com\r\n        RewriteRule ^(.*) \/www\/$1 [L]\r\n\r\n        # Rewrite all other *.oprsteny.cz subdomains\r\n        # to their own directory\r\n        RewriteCond %{HTTP_HOST} ^([^\\.]+)\\.oprsteny\\.com\r\n        RewriteCond \/var\/www\/oprsteny.cz\/subdomains\/%1 -d\r\n        RewriteRule ^(.*) \/subdomains\/%1\/$1 [L]\r\n&lt;\/VirtualHost&gt;<\/pre>\n<\/div>\n<\/div>\n<p>This way, you can just create a directory like<code>\/var\/www\/oprsteny.cz\/subdomains\/blog\/<\/code>\u00a0to make\u00a0http:\/\/blog.oprsteny.cz\u00a0work. Easy like that!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Because I like to play with a lot of projects and code, I create subdomains for oprsteny.cz for almost every project. Instead of going through the hassle of creating a CNAME or A record for every subdomain, creating an Apache &hellip; <a href=\"https:\/\/oprsteny.cz\/?p=195\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"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":[45],"tags":[46,47],"class_list":["post-195","post","type-post","status-publish","format-standard","hentry","category-linux","tag-apache","tag-mod_rewrite"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-39","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=195"}],"version-history":[{"count":2,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/195\/revisions"}],"predecessor-version":[{"id":197,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/195\/revisions\/197"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}