Solved

Customer variables for Mod rewrite and REQUEST_URI

Posted on 2009-04-09
14
1,013 Views
Last Modified: 2013-11-18
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.  
0
Comment
Question by:xiaoyunwu
  • 7
  • 7
14 Comments
 
LVL 27

Expert Comment

by:caterham_www
ID: 24110692
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
 

Author Comment

by:xiaoyunwu
ID: 24110994
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
 
LVL 27

Expert Comment

by:caterham_www
ID: 24111062
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:xiaoyunwu
ID: 24111156
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
 
LVL 27

Expert Comment

by:caterham_www
ID: 24111380
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
 

Author Comment

by:xiaoyunwu
ID: 24111638
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
 
LVL 27

Expert Comment

by:caterham_www
ID: 24111693
That would work but %{HTTP_HOST} wouldn't insert www.randomsite.com but the requested www.mydomain.com.
0
 

Author Comment

by:xiaoyunwu
ID: 24111753
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
 
LVL 27

Expert Comment

by:caterham_www
ID: 24111925
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
 

Author Comment

by:xiaoyunwu
ID: 24115286
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
 
LVL 27

Expert Comment

by:caterham_www
ID: 24120102
$1 contains the value of the matched expression .* from ^/site/(.*)
0
 

Author Comment

by:xiaoyunwu
ID: 24139785
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
 
LVL 27

Accepted Solution

by:
caterham_www earned 500 total points
ID: 24142439
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
 

Author Comment

by:xiaoyunwu
ID: 24250930
It does not match as per my requirements listed.  I have abandoned project.  
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Create and populate iFrame onClick of a button? 1 79
.php tree directory? 5 72
Need To Change Centos 6 Timestamp Form In Log File 24 71
AWS EC2 & RDS Instance 5 35
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question