Mod_rewrite Question


#1

Hi there,

First of all, I should say I am a newbie on whole hosting stuff.

So, here is the question.

I’d like to set something like after you type aaa.xxx.com and press Enter on IE, then it can be redirected to xxx.com/aaa, so I did my homework on Google, and I created a .htaccess file include the following sentences,

RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.] .xxx.org$
RewriteRule ^(. ) %{HTTP_HOST}$1 [C]
RewriteRule ^([^.] ).xxx.org(.*) /$1$2

I put the file in the root of my web address, however, it doesn’t works as when I type aaa.xxx.org, they all go to xxx.org

Anyone can help me to sort this out? Since I think people perfer aaa.xxx.org than xxx.org/aaa

Regards,
Yishi


#2

It seems to me that you are trying to make this a lot harder than it is. Thanks to Dreamhost’s allowing free subdomains, the easiest way for you to have people to go to aaa.xxx.org is to just set up “aaa” as a subdomain of xxx.org.

This can seem a little confusing, as some other web hosts use the www.xxx.org/aaa subdirectory of the xxx.org webspace as a subdomain, but on Dreamhost, aaa.xxx.org can have its very own webspace.

While the end result is the same, it is actually better for your guests, and the server, as no additional re-write requests need to be handled by the server.

If you set up aaa as a subdomain of xxx.org, you will have both xxx.org and aaa.xxx.org as directories within your machine user space. Another advantage this provides is that each can have their own associated email addresses, system software configuration, .htaccess files/rules, etc.

You can still link between the sites, even though you can manage them as two different sites. All in all, it’s just a much easier and more efficient way to do this than using a subdirectory and manipulating urls via Apache re-write rules and .htaccess.

–rlparker


#3

Hi rlparker,

Thanks for your advise, I think it is a very good point that aaa.xxx.org has its own space on DH. But on this practical case, i think if I do your way, then I have to take more works such as rebuild the database, redirect the sitemap and sth like that. Therefore, maybe for my next site, I will using your way but for this time, I still need someone who can help me with the mod_rewirte codes.

Regards, Yishi


#4

Yishi,

I understand. Here is another shortuct that is available on Dreamhost that will allow you to leave your content structure as xxx.org/aaa but allow that sub-directory to function as the subdomain aaa.xxx.org without htaccess/re-write rules:

  1. From the Control Panel, create a New Domain / sub-domain screen, create a new Fully Hosted sub-domain named aaa.xxx.org.

  2. Select the version of PHP you want, whether or not you want to use mod_security (extra web security), fastcgi, etc and select the user to run cgi as (I suggest you use the same user as xxx.org)

  3. In the “Web Directory” field, instead of the “default” value that is there (which should be /home/username/aaa.xxx.org/), enter /home/username/xxx.org/aaa/, and submit the form. Be careful not to add the ending “/” as it is automatically added as indicated). Submit the form.

Once DNS has propagated, all your content in /home/username/xxx.org/aaa will be reachable by either http://aaa.xxx.org or http://xxx.org/aaa.

This way you get the best of both worlds with your exisiting directory structure, and avoid Apache re-write altogether. :wink:

–rlparker


#5

I don’t see anyway for an .htaccess file place in xxx.org will ever get a chance to act on a request for aaa.xxx.org.

Unless there is a DNS entry for aaa.xxx.org, how are you even getting that to redirect to xxx.org?

You could create a subdomain called aaa.xxx.org, and redirect it to xxx.org/aaa from the control panel, but I don’t see how to make what you want to do work, as aaa.xxx.oreg should fail before the .htaccess file in xxx.org gets a chance to re-write any rules (or maybe I am just exceptionally slow tonight :wink: ).

re-write rules are a little like “voodoo magic” to me, and are not my area of specialty. Maybe one of the re-write experts on here can help.

–rlparker


#6

rlparker,

What a great idea, just did what you say, will let you know the result later.

TY,
Yishi


#7

Hello, it works great, thank you for your advise. Regards, Yishi


#8

That’s great news. I’m really glad it worked for you. :slight_smile:

–rlparker