luka8088

Tag: rewrite subdomain

mod_rewrite

by luka8088 on Jun.16, 2009, under Web

Hi, after setting this nice theme, i started organizing the rest on my new site, even before setting up wordpress, i decided to point luka8088.com to one folder (~/public_html), www.luka8088.com to that same folder, and everything else to it’s sub folder, in other words, blog.luka8088.com is located in www.luka8088.com/blog/ (~/public_html/blog), and so on… I wanted to make it all automatically using mod_rewrite:

mod_rewrite is apache module that allows complex url rewriting using regular expressions.

So what I did, since mod_rewrite was already enabled on server, I edited .htaccess file located in ~/public_html and tryed to add some rewrite rules but got many problems, and since I am familiar with regexp, I thought how hard then must be for those people that are not, so I wanted to share some tips:

Rewrite rules can be anywhere in configuration, but I will only speak about them in .htaccess context, basically, it is enough to add:

RewriteEngine On
RewriteRule rewrite-from rewrite-to

Note: If .htaccess does not exists, just create it :)

Example:

RewriteEngine On
RewriteRule ^index\.html$ index.php

This means: if ^index\.html$ is matched to url, it will be replaced with index.php but, not the whole url, if we, for example, open: http://www.example.com/folder/test/index.html,

if .htaccess is located in http://www.example.com/folder/test/
then it will compare regexp with index.html,

if .htaccess is in http://www.example.com/folder/
(test/ doesn’t exists or there is no .htaccess with rewrite rules inside)
then it will compare regexp with test/index.html

and if in http://www.example.com/
then it will compare regexp with folder/test/index.html

And i want to rewrite http://test.example.com/ into http://example.com/test/, so obviously this was not enough… but… I found some other nice directive called RewriteCond :], again… example:

RewriteCond %{HTTP_HOST}abcd%{REQUEST_URI}1234 abcd(.*)$

RewriteCond compares some text (which can contain variables) with regex, and if true, RewriteRule that follows is evaluated, as you can see in example above…

So, I tried:

RewriteEngine On

RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^([^\.]*)[^\/]*(.*)$
RewriteRule .* /%1%2

… now when we try to open: http://abc.example.com/test/ RewriteCond will compare abc.example.com/test/ with given regexp, ([^\.]*) is everything before first dot, that is abc, [^\/]* is everything before first slash, and (.*) is everything else, that is /test/, and we can backreference that parts from RewriteRule using %1 (%1 is backreference from last RewriteCond and $1 is backreference from the same RewriteRule)…

… RewriteRule matches test/ and rewrites it to /abc/test/, so, the new url is: http://abc.example.com/abc/test/ and then new request is issued, and all this rewriting is done again !!!

Basically, what I want to say is, .* in RewriteRule is infinite loop in most cases ! because abc/test/ is next time rewritten to abc/abc/test/ and so on … I wanted to point this out because i had a lot of problems with that.

The solution is to make sure that RewriteRule will rewrite to something that will not match it’s pattern !

Other thing is, I don’t know how to rewriting a domain without browser seeing it, even on the same server, so, pointing all subdomains ( *.luka8088.com ) to the location ( ~/public_html/ ) does the trick, because now, rewriting from http://abc.example.com/test/ to http://www.example.com/abc/test/ is the same as http://abc.example.com/abc/test/ (no domain rewriting is needed)…

For infinite recursion, the best thing I came up with is:

RewriteEngine On

RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^([^\.]*)[^\/]*(.*)$
RewriteRule (^[^\~]|^$) /~%1%2

This means, rewrite only if folder doesn’t start with ~, and rewrite it so it does start with ~, this way, subfolders have to start with ~, for example:

http://abc.example.com/test/ is rewritten to:
http://abc.example.com/~abc/test ( ~/public_html/~abc/test )

There is one restriction about this, and that is, you can’t use ~ as first character of root folder in subdomains, because redirect rule would be skipped…

Related links:

http://www.regular-expressions.info/
http://www.regextester.com/
http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Comments, suggestions and questions are always welcome :)

4 Comments :, , , , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

Archives

All entries, chronologically...