Solved

Customer variables for Mod rewrite and REQUEST_URI

Posted on 2009-04-09
14
1,016 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

726 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