LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   URL Rewriting in htaccess (http://www.linuxquestions.org/questions/linux-general-1/url-rewriting-in-htaccess-816735/)

hatchnet 06-28-2010 12: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
change
somename.mysite.com
to
www.mysite.com?code=somename

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


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


[somename.mysite.com.au/sid#b7917ef0][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
/srv/www/htdocs/mysite/somename/somename/somename/somename/somename/somename/somename/somename/somename/somename/somename/processlogin.php


any takers?

bathory 06-28-2010 02:45 AM

Hi,

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

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.mysite.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-zA-Z]+).mysite.com [NC]
RewriteRule (.*) http://www.mysite.com?code=%1/$1 [L]

Regards

hatchnet 06-28-2010 06: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 07: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 http://www.mysite.com at the beginning of the rewritten url.

Regards

hatchnet 06-28-2010 07:55 AM

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

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

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

bathory 06-28-2010 08:13 AM

In that case the condition is correct, but the rule is still wrong without the http://www.mysite.com at the beginning of the written url.

andrewfeberwee 07-02-2010 01: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

somename.mysite.com.au

but i want it to do

mysite.com.au?var=somename

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 05: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

somename.mysite.com or www.somename.mysite.com
to
www.mysite.com/someprocess.php?code=somename

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} !^www.mysite.com$ [NC]
RewriteCond %{REQUEST_URI} !css [NC]
RewriteCond %{REQUEST_URI} !images [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).mysite.com [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 02:44 AM.