Mod_rewrite and wildcard dns

software development

#1

I’ve looked at about 20 examples, and actually tried about 10 of them.

One of them finally made my entire website stop working and I just got rid of the .htaccess file all together. I’m hoping you guys can help me write it.

This is for the domain antishift.org

I want .antishift.org and www.antishift.org// to lead to http://www.antishift.org/browse.php?disp=user&username=%1

Please help me get this to work! Thanks!


#2

BTW, remember that you need to put in a support ticket to get a wildcard DNS entry for your domain.

Free unique IP and $67 off with promo code [color=#CC0000]FLENSFREEIP67[/color] or use [color=#CC0000]FLENS97[/color] for $97 off. Click here for more options


#3

I already got wildcard DNS on my domain - now I just need to figure out what to put in the .htaccess file - I’ve already tried quite a few things and just can’t get it to work, and was told by support to post here.


#4

So you problem actually has to do with matching the value of “Host” request header. . Uh, you’re going to need to use a back-reference in the RewriteRule substitution string and a RewriteCond pattern that captures the substring of the HTTP_HOST variable. That seems simple enough - I am curious as to see what things you’ve tried.

Also keep in mind that changing the hostname will result in a redirect. This means when the visitor goes to http://username.antishift.org/ he will see the URL in the browser change to http://antishift.org/browse.php?user=username

:cool: openvein.org -//- One-time [color=#6600CC]$50.00 discount[/color] on [color=#0000CC]DreamHost[/color] plans: Use ATROPOS7


#5

I’ve tried things like

RewriteEngine on
RewriteCond %{http_host} .
RewriteCond %{http_host} !^www.antishift.org [NC]
RewriteCond %{http_host} ^([^.]+).antishift.org [NC]
RewriteRule ^(.*)
http://www.antishift.org/browse.php?disp=user&username=%1

I realize it will redirect to the regular domain without the subdomain - that’s what I hope for it to do. I’ve just tried like 10 or so different codes and they either do nothing or mess things up.

Even after deleting my htaccess file, jordan.antishift.org goes to the right page - but any other username.antishift.org goes to the main page, and doesn’t redirect to the www.antishift.org.


#6

Anybody?


#7

RewriteEngine on
RewriteCond %{http_host} ^(w*).antishift.org/(.)$ [NC]
RewriteRule ^(.
)$
http://www.antishift.org/browse.php?disp=user&username=%1 [R=301,L]

Try this I hope I did it right, (This is for permanent redirect btw.)
Silk

My website


#8

nope, still working just like before, where jordan.antishift.org works and nothing else does. :frowning:

any other ideas?


#9

Uh, use upper case for the environment variable names.

Works for me:

[code]RewriteEngine on

don’t redirect for “Host: www.*”

RewriteCond %{HTTP_HOST} !^www

restrict redirects to simple hostnames like test.example.com

RewriteCond %{HTTP_HOST} ^([0-9a-z_-] ).example.com$

rewrite by including previous capture into query string

RewriteRule .* http://example.com/index.pl?sub=%1[/code]
BTW, what is supposed to happen if someone visits

http://username.antishift.org/something/something

Because so far that will still end up as
http://www.antishift.org/browse.php?username=username

Which means the url-path /something/something is lost.

:cool: openvein.org -//- One-time [color=#6600CC]$50.00 discount[/color] on [color=#0000CC]DreamHost[/color] plans: Use ATROPOS7


#10

I tried your code and just changed the variables, making it:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www !^m !^andy
RewriteCond %{HTTP_HOST} ^([0-9a-z_-] ).antishift.org$
RewriteRule .* http://antishift.org/browse.php?disp=user&username=1

still not working :confused:


#11

I don’t beleive you can have multiple nots on the same condition. Each one would be its own condition and put a [OR] at the end of the first two.
Silk

My website


#12

Made it:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www

RewriteCond %{HTTP_HOST} !^m

RewriteCond %{HTTP_HOST} !^andy
RewriteCond %{HTTP_HOST} ^([0-9a-z_-] ).antishift.org$
RewriteRule .* http://antishift.org/browse.php?disp=user&username=1

Got an internal server error.

Tried :

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www !^m !^andy [OR]
RewriteCond %{HTTP_HOST} ^([0-9a-z_-] ).antishift.org$
RewriteRule .* http://antishift.org/browse.php?disp=user&username=1

Rewrite didn’t work.

Went with plain:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{HTTP_HOST} ^([0-9a-z_-] ).antishift.org$
RewriteRule .* http://antishift.org/browse.php?disp=user&username=1

another internal server error

sigh This should be possible…

At first I was thinking it was just gonna be a redirect in the htaccess but DH said it was a mod_rewrite but this isn’t working at all… is there another way to do this?


#13

A not condition is using the ! character before the expression.

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www [OR]
RewriteCond %{HTTP_HOST} !^m [OR]
RewriteCond %{HTTP_HOST} !^andy

My website


#14

Yes, but if it didn’t work with just the !^www, why would it work separating them like that and adding [OR]?

None the less, I changed it and just tried:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www [OR]
RewriteCond %{HTTP_HOST} !^m [OR]
RewriteCond %{HTTP_HOST} !^andy
RewriteCond %{HTTP_HOST} ^([0-9a-z_-] ).antishift.org$
RewriteRule .* http://antishift.org/browse.php?disp=user&username=%1

I got an

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@antishift.org and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.


#15

Ok, one thing I see is your expressions are not complete.
Where ever there is a ^ which means beginning of expression you need a $ for end of expression

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www$ [OR]
RewriteCond %{HTTP_HOST} !^m$ [OR]
RewriteCond %{HTTP_HOST} !^andy$
RewriteCond %{HTTP_HOST} ^([0-9a-z_-] ).antishift.org$
RewriteRule ^(.
)$ http://antishift.org/browse.php?disp=user&username=%1

I am thinking the three not conditions maybe grouped if like this:
RewriteCond %{HTTP_HOST} !^(www|m|andy)$

What may help you as thats what I am looking at is a cheat sheet for mod_rewrite ilovejackdaniels_mod_rewrite cheatsheet

Silk

My website


#16

I thought this would be easy to solve since having wildcard DNS is not necessary in order to do the redirect. A script can do the same thing since scripts can access the HTTP_HOST variable. Also consult the error.log file

Try building this up in steps. In the directives below do not remove ‘example.com’ as we need to make sure the server is not going into a loop trying to redirect to itself. Also instead of having to reload your browser location, go to http://web-sniffer.net/ and type in the URLs there.

Step 1. First get a redirect going:

RewriteEngine on
RewriteRule .* http://example.com/

Try: http://something.antishift.org/something
Expected result: Redirect to http://example.com/

Did that work?

Step 2. Obtain HTTP_HOST value and stuff in substitute URL.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.+)$
RewriteRule .* http://example.com/?host=%1

Try: http://something.antishift.org/something
Expected result: Redirect to http://example.com/?host=something.antishift.org

Did that work?

Step 3. Do not redirect if HTTP_HOST starts with ‘www’

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{HTTP_HOST} ^(.+)$
RewriteRule .* http://example.com/?host=%1

Try: http://something.antishift.org/something
Expected result: Redirect to http://example.com/?host=something.antishift.org
Try: http://www.antishift.org/something
Expected result: No redirect, either 200 OK or 404 Not found if /something does not exist

Did that work?

Step 4. Change substitute hostname

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{HTTP_HOST} ^(.+)$
RewriteRule .* http://www.antishift.org/?host=%1

Try: http://something.antishift.org/something
Expected result: Redirect to http://www.antishift.org/?host=something.antishift.org
Try: http://www.antishift.org/something
Expected result: No redirect, either 200 OK or 404 Not found if /something does not exist

Did that work?

PS. IF you need bypass some hostnames and not others, you need to do:

RewriteCond %{HTTP_HOST} !^(www|andy|m)

:cool: openvein.org -//- One-time [color=#6600CC]$50.00 discount[/color] on [color=#0000CC]DreamHost[/color] plans: Use ATROPOS7


#17

You’re right about that, but you forgot HTTP_HOST is the hostname and may contain more than one label. So the expression should be:

^(www|andy|m).antishift.org$

:cool: openvein.org -//- One-time [color=#6600CC]$50.00 discount[/color] on [color=#0000CC]DreamHost[/color] plans: Use ATROPOS7


#18

sigh Even the first one gave me a 404 error on antishift.org/something, and antishift.org just stayed at the website. Do I need to ask DH to turn the wildcard DNS off?


#19

After having wildcard DNS removed, I tried:

RewriteEngine on
RewriteRule .* http://example.com/

This did nothing to my site.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.+)$
RewriteRule .* http://example.com/?host=%1

This sent www.antishift.org to http://example.com/?host=www.antishift.org - progress!

I then tried:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{HTTP_HOST} ^(.+)$
RewriteRule .* http://example.com/?host=%1

www.antishift.org goes to www.antishift.org, blahh.antishift.org goes to http://example.com/?host=blahh.antishift.org - still working right.

I went ahead and did
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{HTTP_HOST} ^(.+)$
RewriteRule .* http://www.antishift.org/browse.php?disp=user&username=%1

It worked as planned…

However, I then changed it to

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{HTTP_HOST} ^([^.).antishift.org
RewriteRule .* http://www.antishift.org/browse.php?disp=user&username=%1

So it would only catch the subdomain and not the entire thing to forward into %1, and I am now getting a 500 Internal Server Error.


#20

I’ve now tried
RewriteCond %{HTTP_HOST} ^([0-9a-z_-] ).example.com$
and RewriteCond %{HTTP_HOST} ^([^.).antishift.org
and RewriteCond %{HTTP_HOST} ^([0-9a-z_-] *).antishift.org$

and none of them have worked. All 500 internal server errors.