-   Linux - General (
-   -   URL Rewriting in htaccess (

hatchnet 06-28-2010 01:37 AM

URL Rewriting in htaccess
I've been trying tyhis for a while and just don't get it , any url rewrite gurus out there.

OpenSUSE 11 Apache2

Trying to do the following

My latest attempt is
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+) [NC]
RewriteRule (.*) %2/$1 [L]

And I get a loop of which the last line is something like

[][rid#b7fbf260/initial/redir#10] (3) [perdir /srv/www/htdocs/imysite/] add per-dir prefix: somename/somename/somename/somename/somename/somename/somename/somename/somename/somename/somename/processlogin.php

any takers?

bathory 06-28-2010 03:45 AM


Are you sure you want the URL to be rewritten to, i.e. without a trailing slash "/" after the hostname? Because it's not going to work.
Other than that, you can try this:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^$ [NC]
RewriteCond %{HTTP_HOST} ^([a-zA-Z]+) [NC]
RewriteRule (.*)$1 [L]


hatchnet 06-28-2010 07:56 AM

That just so works, I've been mucking about with this for hours, I'm just having trouble assimilating the concept.

Now I wish I know why it works, I will fool around with it some more

Thanks heaps bathory, you're a champ mate :)

bathory 06-28-2010 08:15 AM

Glad to see it works.

It works because both the conditions are met in the first pass. After rewriting, the 1nd condition becomes false, so rewrite stops
Regarding your RewriteRule that is wrong:
There is no%2, as you have only 1 variable in the Rewrite conditions. And you need the at the beginning of the rewritten url.


hatchnet 06-28-2010 08:55 AM

Ahh see,
I had thought that by using
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+) [NC]

The first reference was to the www thing as I was trying to take into account a user might use

That would make the first (www.) my %1 var and the second set my %2, is that wrong?

bathory 06-28-2010 09:13 AM

In that case the condition is correct, but the rule is still wrong without the at the beginning of the written url.

andrewfeberwee 07-02-2010 02:07 AM

This works great for me too? Is there anyway I can make it so that the address bar does not change, for example, I want the bar to say (as in the case above

but i want it to do

Like the above, this stuff is difficult to understand. Does the process cycle around the .htaccess a number of time until there is nothing left to do (looks like 10 times)

I'm doing something like a social networking site for golfers and am trying to give them their own subdomain without altering my dns files

The doco says to do soemthing like

RewriteCond %{HTTP_HOST} ^www\.[^.]+\.host\.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^www\.([^.]+)\.host\.com(.*) /home/$1$2

I have changed this to suit my needs but it is difficult to tell if the rewrite has worked becuase the address bar doesn't change.

The log is not particularly helpful. I am admittedly a bit confusted about when the display url changes and the actual action taken.

hatchnet 07-05-2010 06:57 PM

Just thought I would post a final solution here

My requirement was to simulate a subdomain but without the usual subdirectory requirement which a lot of tutorials post, instead, I wanted the subdomain to appear as an argument as follows or

but still show the subdomain in the address bar, my .htaccess file now looks like this

Options +FollowSymlinks
Options +Indexes
RewriteEngine on

RewriteBase /
RewriteRule ^(.*)\.htm.*$ $1.php [NC]

RewriteCond %{HTTP_HOST} !^$ [NC]
RewriteCond %{REQUEST_URI} !css [NC]
RewriteCond %{REQUEST_URI} !images [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+) [NC]
RewriteRule (.*) $1?var=%2 [NC,QSA]

thanks for the nudge in the right direction. mod_rewrite takes a few days to digest but if you keep at it, eventually you get it (or at least the basics)

All times are GMT -5. The time now is 05:43 AM.