Using Node Sites redirecting to a Base site

New to mod_rewrite? This is a good place to start.

Using Node Sites redirecting to a Base site

Postby Exit » Thu May 21, 2009 10:14 pm

I have a client that wants to have a main site which has all the data for each domain, but have multiple domains hosted at different hosts for better SEO. The solution is to have a proxy script on the node sites that sends an ID to the base site. This isn't the problem.

The problem I'm having is with the mod_rewrite section. The code that the client provided came from a blog with code that didn't actually work.

Here is the original code:
Code: Select all
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/proxy.php
RewriteRule ^(.*)$ proxy.php?secret=123123&u=$1 [PT,L,NE,QSA]


The issue I'm having is that the rewrite will not pass the requesting URLs query string data. Here is the example:

User visits
www.nodesite1.com/page.php?post=1&user=2

mod_rewrite should change this to
www.nodesite1.com/proxy.php?secret=1231 ... t=1&user=2

proxy.php then processes.

My first guess at the problem is that there are two '?' in the URL. Entering the URL directly shown above will work, but that is probably due to the browser doing a URL encoding on the second '?'.

Any ideas?
Exit
 
Posts: 2
Joined: Thu May 21, 2009 8:52 pm

Postby Exit » Fri May 22, 2009 12:19 am

Wait a minute...

I was trying to hard to use someone else's idea. Why pass the REQUEST_URI via a redirect? PHP will provide the same URI from the $_SERVER['REQUEST_URI'] variable. This solves the problem above, the answer is:

Code: Select all
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/proxy.php
RewriteRule ^(.*)$ proxy.php?secret=123123
Exit
 
Posts: 2
Joined: Thu May 21, 2009 8:52 pm

Postby richardk » Fri May 22, 2009 8:22 am

The issue I'm having is that the rewrite will not pass the requesting URLs query string data.

The part of the URL matched by RewriteRules does not include the query string (or the domain name). In .htaccess files file and <Directory>s it is also relative to the directory the .htaccess file is in so if it's /abc/.htaccess and the request is for /abc/def?ghi the RewriteRule should match def only.

You can access the query string with the %{QUERY_STRING} variable.

This solves the problem above, the answer is:

You should keep the L flag
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule !^proxy\.php$ /proxy.php?secret=123123 [L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 113 guests

cron