2
   

mod_rewrite and wildcard DNS

 
 
Echilon
 
Reply Mon 28 May, 2007 04:15 am
I'm having problems with rewriterule. Ity's always a buit hit and miss, but I can't figure out why it's not working this time. I wan to change from having a load of arguments in the URL to having a directory/subdirectory structure. I have wildcard DNS enabled, and the code below rewrites every subdomain to an argument - eg: http://blah.mydomain.net/ becomes http://mydomain.net/index.php?cal=blah.
Quote:

RewriteCond %{HTTP_HOST} !^(www|images)\.mydomain\.net$
RewriteCond %{HTTP_HOST} !^mydomain\.net$
RewriteCond %{HTTP_HOST} ^([^\.]+)\.mydomain\.net$
RewriteRule (.*) /home/mydomain/index.php?cal=%1

The problem is, I want to pass more paramaters, so http://blah.mydomain.net/rss/ becomes http://mydomain.net/index.php?cal=blah&act=rss. I've tried using another rewrite rule, but I can't get it working. I *think* it should be something like this:
Quote:

RewriteCond %{HTTP_HOST} !^(www|images)\.mydomain\.net$
RewriteCond %{HTTP_HOST} !^mydomain\.net$
RewriteCond %{HTTP_HOST} ^([^\.]+)\.mydomain\.net$
RewriteRule ^rss/?$ /home/mydomain/index.php?cal=%1&act=rss [S,L]
RewriteRule (.*) /home/mydomain/index.php?cal=%1

Could anyone lend a hand?
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Discussion • Score: 2 • Views: 5,392 • Replies: 6
No top replies

 
Craven de Kere
 
  1  
Reply Mon 28 May, 2007 04:39 am
Re: mod_rewrite and wildcard DNS
Echilon wrote:
Ity's always a buit hit and miss, but I can't figure out why it's not working this time.


Server logs are always the first place I look when debugging rewrite directives.

I haven't slept in a few days and can't help but think this is either not going to work or at least far from the best way to do it, but give it a try.

Code:
RewriteEngine On

# Verify subdomain is not www or images
RewriteCond %{HTTP_HOST} !^(www|images) [NC]
# Extract the subdomain AND sub directory
RewriteCond %{HTTP_HOST} ^([^.]+)\.yourdomain\.net/(.*)/[NC]
# Pass subdomain and subdirectory to get variable
RewriteRule ^$ /home/mydomain/index.php?cal=%1&act=%2 [L]

# Verify subdomain is not www or images
RewriteCond %{HTTP_HOST} !^(www|images) [NC]
# Extract the subdomain part
RewriteCond %{HTTP_HOST} ^([^.]+)\.yourdomain\.net[NC]
# Pass subdomain to get variable
RewriteRule ^$ /home/mydomain/index.php?cal=%1 [L]

0 Replies
 
Echilon
 
  1  
Reply Mon 28 May, 2007 07:00 am
It's a start, but it's not quite working properly.
Code:
RewriteCond %{HTTP_HOST} !^(www|images)\.mydomain\.net$ [NC]
RewriteCond %{HTTP_HOST} !^mydomain\.net$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.net/ [NC]
RewriteCond %{REQUEST_URI} ^/(rss|manage)/?(.*)$ [NC]
RewriteRule ^(.*)$ /home/hj/mydomain/current/index.php?cal=%1&act=%2 [L]

RewriteCond %{HTTP_HOST} !^(www|images)\.mydomain\.net$ [NC]
RewriteCond %{HTTP_HOST} !^mydomain\.net$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.net$ [NC]
RewriteRule ^(.*)$ /home/hj/mydomain/current/index.php?cal=%1 [L]

When I use PHP to do a var_dump or $_GET on http://blah.mydomain.net/manage/, it shows
Quote:

array(2) { ["cal"]=> string(4) "demo" [""]=> NULL }

$_SERVER['REQUEST_URI'] is /manage/, yet the second rule is the one being enforced. The first block is ignored.
0 Replies
 
Craven de Kere
 
  1  
Reply Tue 29 May, 2007 01:28 am
Your code is different. What wasn't working with my code? Can you post your error log when my code is used?
0 Replies
 
Echilon
 
  1  
Reply Tue 29 May, 2007 02:38 am
Your code was sending me back to the default page (mydomain.net) as though no parameters had been passed. I assumed it was because you were trying to check for subdirectories in HTTP_HOST.
0 Replies
 
Echilon
 
  1  
Reply Fri 1 Jun, 2007 01:14 pm
Anyone?
0 Replies
 
Monger
 
  1  
Reply Fri 1 Jun, 2007 06:14 pm
Echilon wrote:
It's a start, but it's not quite working properly.
Code:
RewriteCond %{HTTP_HOST} !^(www|images)\.mydomain\.net$ [NC]
RewriteCond %{HTTP_HOST} !^mydomain\.net$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.net/ [NC]
RewriteCond %{REQUEST_URI} ^/(rss|manage)/?(.*)$ [NC]
RewriteRule ^(.*)$ /home/hj/mydomain/current/index.php?cal=%1&act=%2 [L]

RewriteCond %{HTTP_HOST} !^(www|images)\.mydomain\.net$ [NC]
RewriteCond %{HTTP_HOST} !^mydomain\.net$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.net$ [NC]
RewriteRule ^(.*)$ /home/hj/mydomain/current/index.php?cal=%1 [L]

When I use PHP to do a var_dump or $_GET on http://blah.mydomain.net/manage/, it shows
Quote:

array(2) { ["cal"]=> string(4) "demo" [""]=> NULL }

$_SERVER['REQUEST_URI'] is /manage/, yet the second rule is the one being enforced. The first block is ignored.


Replace the errant "/" at the end of your third RewriteCond with "$". Additionally, you can get rid of of both lines which read:

Code:RewriteCond %{HTTP_HOST} !^mydomain\.net$ [NC]

They add nothing but performance overhead because the following RewriteCond already requires a subdomain (note that Craven didn't include it in his code, probably for just this reason).

I've never written a .htacccess file in my life, so I can't analyze it much further than that without having to reference the Apache docs.
0 Replies
 
 

Related Topics

Webdevelopment and hosting - Question by harisit2005
Showing an Ico File - Discussion by Brandon9000
how to earn money in internet - Discussion by rizwanaraj
The version 10 bug. Worse then Y2K! - Discussion by Nick Ashley
CSS Border style colors - Question by meesa
There is no Wisdom in Crowds - Discussion by ebrown p
THANK YOU CRAVEN AND NICK!!! - Discussion by dagmaraka
I'm the developer - Discussion by Nick Ashley
 
  1. Forums
  2. » mod_rewrite and wildcard DNS
Copyright © 2024 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.03 seconds on 04/24/2024 at 05:19:11