views:

24

answers:

2

I am trying to use mod_rewrite to redirect a domain in the site URL to a php script. For example:

http://example.com/http://somedomain.com

should redirect to:

http://example.com/test.php?domain=http://somedomain.com

I have tried to do this with the following RewriteRule

RewriteEngine On
RewriteRule ^(.*)$ /test.php?domain=$1 [L]

but I am having no luck. Does anyone know the correct RewriteRule to use or is there a better method of accomplishing this?

Thanks

A: 

It's possible to do this, but only if the RewriteRule is specified in a per-server context, which means that you have to be able to edit the configuration for your VirtualHost or the server in general (httpd.conf). In this case, the rule you have would work fine.

Defining the rule in .htaccess doesn't work, because this occurs in the fixup phase of Apache's request processing, which is after Apache tries to map the request to the filesystem (and therefore after Apache errors out the request). If you don't have access to the necessary configuration files, the most obvious workaround for this would to simply not pass the http:// scheme as part of the URL.

Your rule would still work, but because of how mod_rewrite works in a per-directory context, you should probably condition it as well to prevent a redirect loop (the L and explicit capture group are also unnecessary):

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ /test.php?domain=$0

As far as downforeveryoneorjustme.com goes, it's running on the Google Frontend server, which is reportedly a modified version of Apache (though I didn't do much checking into this myself), so I imagine they've gone with the first option, or have some entirely different system; I'm not sure.

Tim Stone
A: 

Just figured this out (a month later!) and decidede to share so it might help someone else.

Use the following .htaccess file to redirect all requests to your index.php file (or wherever you want).

<IfModule mod_rewrite.c>

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

</IfModule>

now we can use $_SERVER['REQUEST_URI'] to get the requested URI.

For example if the requested URL is http://example.com/http://google.com, $_SERVER['REQUEST_URI'] will be set to 'http://google.com'