Forum Moderators: phranque

Message Too Old, No Replies

RewiteRule Problem with url logic

mod_rewrite problem with url logic

         

spittenger

8:22 am on Jul 28, 2008 (gmt 0)

10+ Year Member



I am not very good at .htaccess mod_rewite apparently. I am decent at programming php but i am baffled at this particular deal...

i have a rewrite rule in the .htaccess that i created:

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1/[L,R]

when i type in http://subdomain-name.example.com i get http://example.com/subdomain-name/

That is fine but what i want to do is type in :

http://subdomain-name.example.com/clientname and have it go to a page ive created called visit.php passing the subdomain-name as "cid" and the clientname as "lid"

i.e.

http://example.com/campaigns/clients/subdomain-name/visit.php?cid=clientname&lid=subdomain-name

what can i do ...

i am baffled and its late... please help if you can,

Thanks

[edited by: jdMorgan at 2:28 pm (utc) on July 28, 2008]
[edit reason] example.com [/edit]

jdMorgan

2:26 pm on Jul 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please post your best-effort code as a basis for discussion. More information and links to resources are available in our Forum Charter -- See link at top left of this page.

There are also many previous threads on this subject. A site search on "rewrite subdomain to script" and similar phrases will likely turn up some threads that prove useful.

Jim

spittenger

4:29 pm on Jul 28, 2008 (gmt 0)

10+ Year Member



The code ive come up with is:

RewriteRule ^(.*)/(.*)$ [mydomain.com...] [L,R]

but this generates an error

please help

spittenger

4:32 pm on Jul 28, 2008 (gmt 0)

10+ Year Member



This part works:

RewriteRule ^(.*)$ [mydomain.com...]

to get to [mydomain.com...]

thanks

g1smd

6:50 pm on Jul 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Beware that [R] will give a 302 response. You'll likely want a 301 instead.

jdMorgan

7:11 pm on Jul 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And it appears that what is wanted is an internal rewrite anyway. Something like this, perhaps:

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com [NC]
RewriteRule ^([^/.]+)$ /campaigns/clients/subdomain-name/visit.php?cid=$1&lid=%1 [L]

As shown, client names may not contain a slash or a period (full stop). This is to prevent confusion with existing files on your server without requiring an inefficient "file exists" check for each and every request.

Jim