?
Solved

Rewrite all subdomains to similar urls. Mod_Rewrite

Posted on 2009-04-30
5
Medium Priority
?
608 Views
Last Modified: 2012-05-06
I want to rewrite all subdomains to similar urls. See examples below:

this.that.example.com >> example.com/that/this/
anything.example.com >> example.com/anything/
a.b.c.d.example.com >> example.com/d/c/b/a/

I understand that a RewriteMap has to be written in httpd.conf but I have no idea how to and I don't have the time to Google and write it myself. Any help will be greatly appreciated.

I am open to solutions other than RewriteMap if they exist.
0
Comment
Question by:stmayes
  • 3
  • 2
5 Comments
 
LVL 27

Expert Comment

by:caterham_www
ID: 24268442
I wrote a map script some long time ago in 2006 which should do the transformation you need.
# httpd.conf
RewriteEngine on
# we define the map and specify the physical path to our perl script
RewriteMap sub /physical/path/to/rwmap.pl
# we specify an existing path where apache has write-access
# in order to create a lock file and lock the file for map synchronization
RewriteLock  /physical/path/to/map.sub.lock
 
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z.-]+)\.example\.com
# we don't want to hit "www"
RewriteCond %2 !=www
# we translate the request to a physical path
RewriteRule ^/(.*) /home/www/htdocs/${sub:%2}$1 [L]
 
 
 
 
# perl script rwmap.pl:
 
#!/usr/bin/perl
$| = 1;
while(<STDIN>)
{
chomp($_);
my @lables = split(/\./);
@lables = reverse @lables;
foreach my $label (@lables) {
        $path .= $label . "/";
        }
print $path . "\n";
}

Open in new window

0
 

Author Comment

by:stmayes
ID: 24268486
Arh. One thing I forgot to add was that I am currently using the Zend Framework (PHP) and I use the following code to rewrite the url and send everything via index.php.

I need all the new URLs to be feed via index.php
RewriteEngine on
RewriteBase /
RewriteRule .* index.php

Open in new window

0
 
LVL 27

Accepted Solution

by:
caterham_www earned 2000 total points
ID: 24268529
So you'd like to redirect (i.e. url changes in the browser) first (otherwise the mapping would be lost if you're rewriting to index.php again.?
# httpd.conf
RewriteEngine on
# we define the map and specify the physical path to our perl script
RewriteMap sub /physical/path/to/rwmap.pl
# we specify an existing path where apache has write-access
# in order to create a lock file and lock the file for map synchronization
RewriteLock  /physical/path/to/map.sub.lock
 
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z.-]+)\.example\.com
# we don't want to hit "www"
RewriteCond %2 !=www
# we redirect the request
RewriteRule ^/(.*) http://www.example.com/${sub:%2}$1 [R=301,L]

Open in new window

0
 

Author Comment

by:stmayes
ID: 24268721
I would like the subdomains to redirect to the urls. Then I would like all urls to point to index.php, so it can parse the url and display the correct resources.
0
 
LVL 27

Expert Comment

by:caterham_www
ID: 24271675
That should be no problem; the rule above will do the redirect. Your other rule is used in directory context and won't conflict.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my time as an SEO for the last 2 years and in the questions I have assisted with on here I have always seen the need to redirect from non-www urls to their www versions. For instance redirecting http://domain.com (http://domain.com) to http…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
Suggested Courses
Course of the Month6 days, 9 hours left to enroll

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question