Customer variables for Mod rewrite and REQUEST_URI

I am trying to find a rewrite condition or rule to let me create a custom variable rather than just using the standard REQUEST_URI.  

Out of the following URL, I need anything after "www.mydomain.com/site/" but before "?Site=www.mysite.com" to be in a variable that I can use in a rewrite rule.  

www.mydomain.com/site/home?Site=www.mysite.com


So my rewrite rule would looking something like this:

RewriteRule (.*) http://%{HTTP_HOST}/site/%{Custom_variable}?Site=www.mysite.com

Is there any way to do this with Mod rewrite or something similar?  

The Custom_variable would equal "home" for the URL above.  
xiaoyunwuAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
caterham_wwwConnect With a Mentor Commented:
Have you tried the regular expression in a RewriteRule in your httpd.conf? If yes, you'd see from your rewriteLog that the backreference contains the desired match.
0
 
caterham_wwwCommented:
Extract the value from the pattern, but you your construct will loop
# httpd.conf
RewriteRule ^/site/(.*) http://%{HTTP_HOST}/site/$1?Site=www.mysite.com
 
# .htaccess
RewriteRule ^site/(.*) http://%{HTTP_HOST}/site/$1?Site=www.mysite.com

Open in new window

0
 
xiaoyunwuAuthor Commented:
How would I write the condition then?  The part I am having trouble with is creating the variable with the required content shown above?



# httpd.conf
RewriteRule ^/site/(.*) http://%{HTTP_HOST}/site/$1?Site=www.mysite.com
 
# .htaccess
RewriteRule ^site/(.*) http://%{HTTP_HOST}/site/$1?Site=www.mysite.com
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
caterham_wwwCommented:
What is your intention of the rewrite?

You're rewriting http://www.mydomain.com/site/home?Site=www.mysite.com to http://www.mydomain.com/site/home?Site=www.mysite.com (=same URL) or not?

$1 contains the value "home".

0
 
xiaoyunwuAuthor Commented:
My issue is figuring out how to create the condition that creates the $1.  The end result is going to change each time based on the input from that variable.  The $1 needs to be the content after
 "www.mydomain.com/site/" but before "?Site=www.mysite.com".  

So, this URL, http://www.mydomain.com/site/home?Site=www.mysite.com 
the variable would be: home
This URL: http://www.mydomain.com/site/news?Site=www.mysite.com 
The variable would be: news
This URL: http://www.mydomain.com/site/etc?Site=www.mysite.com 
The variable would be: etc
etc

RewriteRule ^/site/(.*) http://%{HTTP_HOST}/site/$1?Site=www.mysite.com

0
 
caterham_wwwCommented:
Yes, $1 contains the different values, but the rule would be an infinite loop since you're redirecting to the same location and not to a different host or a different path.
0
 
xiaoyunwuAuthor Commented:
No, the $1 will be changing every time which is why I need the variable.  If I can find the correct way to create the variable based on the criteria listed, then the rule will redirect to a different site every time based on the variable.  

Lets say I'm browsing a theoretical site.
http://www.mydomain.com/site/home?Site=www.mysite.com 

I want to be able to redirect the above URL to another URL based on the "home" section changing each time to:

http://www.randomsite.com/site/{this is where my variable goes $1}/?Site=www.mysite.com

My question is how do I create the variable that will be defined as the data after "http://www.mydomain.com/site/" but before "?Site=www.mysite.com"
0
 
caterham_wwwCommented:
That would work but %{HTTP_HOST} wouldn't insert www.randomsite.com but the requested www.mydomain.com.
0
 
xiaoyunwuAuthor Commented:
HTTP_HOST gives me the base domain.  www.mydomain.com
Request_URI gives me:  /site/$1?Site=www.mysite.com

But I need a variable that gives me only:  home/news/etc... defined as the data after "http://www.mydomain.com/site/" but before "?Site=www.mysite.com"

For this URL:  www.mydomain.com/site/home?Site=www.mysite.com
The variable in should pull "home" so I can post the variable into my rewrite rule.  


RewriteRule ^/site/(.*) http://%{HTTP_HOST}/site/$1?Site=www.mysite.com
0
 
caterham_wwwCommented:
Yes, that is done by $1 but you must not reference %{HTTP_HOST} but something like

RewriteRule ^/site/(.*) http://othersite.com/site/$1?Site=www.mysite.com
0
 
xiaoyunwuAuthor Commented:
Ok, but my question is how do I make the condition that lets me create the variable $1 as per the requirements I listed above?  I understand how to do the rewrite rule once i have the variable, but I can't figure out how to make the variable.  
0
 
caterham_wwwCommented:
$1 contains the value of the matched expression .* from ^/site/(.*)
0
 
xiaoyunwuAuthor Commented:
I understand that; however, how do I create the "matched expression" to meet the following criteria:

Defined as the data after "http://www.mydomain.com/site/" but before "?Site=www.mysite.com"
0
 
xiaoyunwuAuthor Commented:
It does not match as per my requirements listed.  I have abandoned project.  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.