LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 06-28-2010, 12:37 AM   #1
hatchnet
LQ Newbie
 
Registered: Jun 2010
Posts: 4

Rep: Reputation: 0
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?
 
Old 06-28-2010, 02:45 AM   #2
bathory
Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,937

Rep: Reputation: 1330Reputation: 1330Reputation: 1330Reputation: 1330Reputation: 1330Reputation: 1330Reputation: 1330Reputation: 1330Reputation: 1330Reputation: 1330
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
 
Old 06-28-2010, 06:56 AM   #3
hatchnet
LQ Newbie
 
Registered: Jun 2010
Posts: 4

Original Poster
Rep: Reputation: 0
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
 
Old 06-28-2010, 07:15 AM   #4
bathory
Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,937

Rep: Reputation: 1330Reputation: 1330Reputation: 1330Reputation: 1330Reputation: 1330Reputation: 1330Reputation: 1330Reputation: 1330Reputation: 1330Reputation: 1330
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
 
Old 06-28-2010, 07:55 AM   #5
hatchnet
LQ Newbie
 
Registered: Jun 2010
Posts: 4

Original Poster
Rep: Reputation: 0
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?
 
Old 06-28-2010, 08:13 AM   #6
bathory
Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,937

Rep: Reputation: 1330Reputation: 1330Reputation: 1330Reputation: 1330Reputation: 1330Reputation: 1330Reputation: 1330Reputation: 1330Reputation: 1330Reputation: 1330
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.
 
Old 07-02-2010, 01:07 AM   #7
andrewfeberwee
Member
 
Registered: Feb 2007
Posts: 30

Rep: Reputation: 15
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.

Last edited by andrewfeberwee; 07-02-2010 at 01:33 AM.
 
Old 07-05-2010, 05:57 PM   #8
hatchnet
LQ Newbie
 
Registered: Jun 2010
Posts: 4

Original Poster
Rep: Reputation: 0
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)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
URL rewriting/Centos lonster Linux - Newbie 1 09-28-2009 08:46 AM
Squid - URL Rewriting nitrohuffer2001 Linux - Enterprise 1 10-23-2008 07:44 PM
URL Rewriting mrpraveen.p Linux - Newbie 1 06-03-2008 07:07 AM
URL Rewriting getmeontop Linux - Newbie 1 04-02-2008 02:12 AM
url rewriting in localhost pamount Red Hat 0 01-08-2008 08:10 PM


All times are GMT -5. The time now is 10:18 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration