Solved

Customer variables for Mod rewrite and REQUEST_URI

Posted on 2009-04-09
14
1,011 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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now