Forum Moderators: phranque

Message Too Old, No Replies

subdomain rewite challenge

newbie giving it a try...

         

kent88

8:03 pm on Feb 19, 2008 (gmt 0)

10+ Year Member



Hi,

I am trying to turn this --> newyork.domain.com into this --> testsite.domain.com/index.php?region=245

I have wildcard subdomains setup on my server so you can type in anything like: anywhere.domain.com and get the content of testsite.domain.com so that is working correctly.

I have a text file (map-list.txt)with the contents in this format:

newyork 245
losangeles 246
boston 247
...

Here is the last few lines of httpd.conf

RewriteEngine on
RewriteMap city txt:/home/username/public_html/testsite/map-list.txt
RewriteCond %{HTTP_HOST} ^([^.]+).domain.com$
RewriteRule ^(.*)$ index.php?region=${city:%1¦http://domain.com/nocity.htm}

But it does not change the region when I go to newyork.domain.com

Any direction on this would be appreciated.
Kent

wilderness

8:39 pm on Feb 19, 2008 (gmt 0)

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



There was a recent thread in which the inquiree listed approimately 25 sub-folder references 2-3 times in the thread which may assist you.

kent88

10:50 pm on Feb 19, 2008 (gmt 0)

10+ Year Member



Thanks... I will do some more research and reading then.

Kent

machonemedia

12:06 am on Feb 20, 2008 (gmt 0)

10+ Year Member



I have a very small problem and don't want to open another thread (there are enough subdomain threads).

RewriteEngine on

RewriteCond %{HTTP_HOST} ([^\.]+)\.site\.com [NC]

RewriteRule ^profile/([0-9]+)/$ profile.php?id=$1&company=%1 [NC]

# Redirect main page to login page if accessed
RewriteCond %{HTTP_HOST} ^http://(!www.¦(.*))?site.com$
RewriteRule ^(/)?$ /login/ [R]

This code did work at one point, but now it's not. This should get the subdomain string and put it into the PHP variable $_GET['company']; (which it did when I was first setting up my site).

Now it goes to the main page of my site when I enter [companyname.site.com...] instead of redirecting to [companyname.site.com...]

Any ideas on my syntax? I added the "!www." so it wouldn't redirect to the login page if www is present. Also wouldn't redirect when [site.com...] is entered. I have a wildcard subdomain setup on my server and a demo.site.com subdomain which has it's own directory for it's code and stuff though (and it works).

I just need [companyname.site.com...] to redirect to a login page (I also have other backend pages that need to track the $_GET['company'] variable, so it must be able to be passed to all following rules (which it did at one time).

And [site.com...] and [site.com...] should go to my main index.php page which it does.

Thanks in advance for any help, I hope it's a simple syntax fix because I have quite a few rules following the condition.

jdMorgan

4:12 am on Feb 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You cannot use "!" as anything but the first token in a mod_rewrite pattern, so that RewriteCond will only match if there is a literal "!" preceding "www" in the hostname requested by the client, or if the root domain is requested.

Also, you cannot back-reference a negative-match pattern, because when the pattern match is resolved as "True", the back-reference will always be empty.

You'll probably need to break the two hostname checks into two separate RewriteConds, one with the NOT
pattern to disable the rule, and the other to create the desired %1 back-reference.

Jim

machonemedia

8:25 pm on Feb 20, 2008 (gmt 0)

10+ Year Member



Thanks jdMorgan,

I managed to get this working, adding multiple conditions to catch www attempts, etc..

The only thing wrong is my %1 variable doesn't get passed through to my following rules anymore. Should I setup an environment variable so it's available to all rules in my htaccess file? I can't find how to do that.

RewriteEngine on

RewriteCond %{HTTP_HOST} !^(www\.)site.com$ [NC]
RewriteCond %{HTTP_HOST} !^(.*)\.site.com$ [NC]
RewriteRule ^ [%{HTTP_HOST}%{REQUEST_URI}...] [L,R=301]

RewriteCond %{HTTP_HOST} ^(.*)\.site\.com [NC]

RewriteRule ^profile/([0-9]+)/$ profile.php?id=$1&company=%1 [NC]
RewriteRule ^profile/([0-9]+)/([0-9]+)/$ profile.php?id=$1&nw=$2&company=%1 [NC]

RewriteRule ^profile/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/$

# Redirect main page to login page if accessed
RewriteCond %{HTTP_HOST} !^(www\.)site.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.site.com$
RewriteRule ^(/)?$ /login/ [R]

Do you see anything that catches your eye?

Thanks,
Bryan

kent88

8:45 pm on Feb 20, 2008 (gmt 0)

10+ Year Member



Jim,

Any thoughts on my post?

Kent

jdMorgan

3:33 am on Feb 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The only thing that came to mind reading the first post in this thread was, "I hope you're completely flushing your browser cache between tests." If not, you'll be seeing the previously-cached results until the cache expires.

Jim

kent88

4:02 am on Feb 21, 2008 (gmt 0)

10+ Year Member



Thanks... I did delete the cashe in the browser and I have been restarting apache each time but still no success. I will tinker some more and post if I figure it out. Do you think the syntax is clean?

Kent

jdMorgan

5:51 am on Feb 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest a few changes:

RewriteEngine on
RewriteMap city txt:/home/username/public_html/testsite/map-list.txt
RewriteCond %{HTTP_HOST} ^([^.]+)[b]\.d[/b]omai[b]n\.c[/b]om
RewriteRule !^/([^/]+/)*[^.]*\.[^./])?$ /index.php?region=${city:%1¦[b]/nocity.htm} [L][/b]

I also changed the rule pattern to reject URL-paths with filetypes in them, on the assumption that you don't really want to rewrite *all* URLs in each subdomain to one file -- robots.txt? style.css? logo.gif? .htaccess? Change it back if you really do want to rewrite all requests.

Note that posting on this forum modifies the pipe characters; Change any/all broken pipes "¦" back to solid pipes before trying to use any of this code.

Jim

kent88

6:39 am on Feb 21, 2008 (gmt 0)

10+ Year Member



Thanks,
With these new changes I am unable to restart apache.

Kent

kent88

6:46 am on Feb 21, 2008 (gmt 0)

10+ Year Member



But... I'm not exactly sure if it is the rewriterule changes that are the issue... yet...

kent88

6:59 am on Feb 21, 2008 (gmt 0)

10+ Year Member



yup... if I comment (#) out those lines then apache restarts.

machonemedia

8:45 am on Feb 21, 2008 (gmt 0)

10+ Year Member



Hey jdMorgan. Don't mean to sound bothersome but do you have any advice for my code?

kent88

3:12 pm on Feb 21, 2008 (gmt 0)

10+ Year Member



It seems to have an issue with this part of the rewriterule line:

!^/([^/]+/)*[^.]*\.[^./])?$

Thanks
Kent

jdMorgan

4:07 pm on Feb 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please be aware that individual members here are often in different time zones, and don't spend all day here. Further, all moderators and admins are unpaid volunteers. Your patience will be appreciated.

Kent,
Sorry, I missed a left parenthese in there, somehow.


RewriteRule !^/([^/]+/)*[b]([[/b]^.]*\.[^./])?$ /index.php?region=${city:%1¦/nocity.htm} [L]

When you get an error, see your server error log -- It will often tell you exactly what is wrong.

Macho,
The original poster is the owner of a thread, and responses to the owner's posts take priority. In future, please open your own thread (Thanks!).

RewriteConds and back-references in those RewriteConds apply only to the single RewriteRule following those RewriteConds. A useful general construct is:


RewriteCond %{some_var} (some_var_pattern)
RewriteRule some_URL_pattern - [E=uvarname:%1]

This tests the specified server variable and creates a back-reference, then copies that back-referenced value into the named "user" variable, leaves the URL unchanged, and continues with mod_rewrite processing. The user variable can then be de-referenced using

RewriteCond %{ENV:uvarname} some_pattern

or it can be directly included into the substitution URL in the RewriteRule:

RewriteRule some_URL_pattern substitution_URL_head_%{ENV:uvarname}_substitution_URL_tail [flags]

See Apache mod_rewrite documentation for more info.

Jim

kent88

4:19 am on Feb 22, 2008 (gmt 0)

10+ Year Member



I am able to restart apache now. Thanks for the correction. It still does not change the region though. I added a rewritelog and it creates the file rewritelog.txt but the file is empty. There are no errors in the errorlog.

I appreciate your suggestions and assistance. I might have to hire someone to look deeper into this.
Cheers,
Kent

jdMorgan

5:31 pm on Feb 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If rewritelog is empty, see RewriteLogLevel -- It must be set to a non-zero level in order to log anything. Be sure to set this back to zero when finished testing!

Jim

jdMorgan

5:35 pm on Feb 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Another suggestion: Break this down into two or more parts. Test a static rewrite first (remove the rewritemap call and use a static rule as if coding for each URL case-by-case). If that works, then you'll at least know "which half" of the functions are causing the problem.

I often suggest trying a dirt-simple rule when folks have never had any working rewriterule in their configuration... something like
RewriteRule ^foo\.html$ http://www.google.com [R=301,L]

So, carrying on that idea, divide and conquer!

Jim

kent88

4:33 pm on Feb 23, 2008 (gmt 0)

10+ Year Member



Hi,
OK... I learned some new things so that is good. I put the rewriterules between the <VirtualHost *> and </VirtualHost> lines and I now get a 500 Internal Server Error.
I have the RewriteLogLevel set to 5 and I am getting data sent to it that indicates that it is reading the map file and assigning key=newyork to val=245 correctly.
It looks like the 500 error is due to an infinite rewrite loop. Towards the end of the log file I have lines like this:

(2) init rewrite engine with requested uri /cgi-sys/php5/cgi-sys/php5/cgi-sys/php5/cgi-sys/php5/cgi-sys/php5/cgi-sys/php5/cgi-sys/php5/cgi-sys/php5/cgi-sys/php5/cgi-sys/php5/favicon.ico

Getting closer though....
Kent