homepage Welcome to WebmasterWorld Guest from 54.237.99.131
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Strip variable from url rewrite rule
racerx8413



 
Msg#: 4558390 posted 7:06 pm on Mar 25, 2013 (gmt 0)

I can't quite figure this one out, I'm trying to strip a section out of a url, the url can differ due to a variable in the middle:

jump.php?dbID=966&ban_www=/company-detail.php?com_id=74650

needs to be rewritten to:

company-detail.php?com_id=74650

I've tried several variations - basically
^jump.php?dbID=(.*)&ban_www=/company-detail.php?com_id=(.*) company-detail.php?com_id=$2

I probably just don't know what I'm doing - the only result I can get out of any variation I've tried is just "company-detail.php" with no $2

 

racerx8413



 
Msg#: 4558390 posted 7:11 pm on Mar 25, 2013 (gmt 0)

Wow - all morning I've been messing with this - just figured it out. My rewrites work - the problem is that the url is depositing itself into a frame. I just tried with the main url - works great.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4558390 posted 9:48 pm on Mar 25, 2013 (gmt 0)

I am avidly awaiting a follow-up post, because frankly I don't see how the rule can work-- frame or no frame. The query string isn't visible to mod_rewrite unless you put it in a RewriteCond.

racerx8413



 
Msg#: 4558390 posted 9:54 pm on Mar 25, 2013 (gmt 0)

no conditions for any of the rules - this is what I ended up using

RewriteRule ^jump.php?dbID=(.*)&ban_www=/company-detail.php?com_id=(.*)$ company-detail.php?com_id=$2 [R=301,L,QSA]

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4558390 posted 10:09 pm on Mar 25, 2013 (gmt 0)

Never use (.*) at the beginning or in the middle of a RegEx pattern.

(.*) means "match everything remaining" - that means everything to the end, i.e. there will be nothing after it.

Be aware that a rewrite takes an incoming URL request and "rewrites" Apaches internal pointer to point to a specific folder and file in the internal filesystem. Rewrites are internal to the server.

A rewrite does not affect URLs in any way whatsoever. URLs are used out there on the web and have no meaning inside the server.

On the other hand, a redirect tells the requester to make a new request for a different URL. But again it doesn't "change" a URL, it merely tells you to request a different one.

In your original question, you asked for a "rewrite" but the code in your last post is for a "redirect".

Whatever you want, the rule you posted can never work. The RegEx pattern in a RewriteRule can match only the path part of the URL request. It cannot ever match requested query strings. Those are matched using a preceding RewriteCond looking at either QUERY_STRING or THE_REQUEST.

If you want a redirect, the rule target should include protocol and hostname.

A redirect maps a URL to a new URL.

A rewrite maps a URL to an internal filepath.

Be sure you know what you want. Redirects and rewrites are both coded using RewriteRules with only minor differences in syntax.

racerx8413



 
Msg#: 4558390 posted 10:22 pm on Mar 25, 2013 (gmt 0)

My apologies for using the incorrect form (redirect/rewrite) I think a rewrite is what I was looking for.

I'm open to suggestions on cleaning that up, the rule however does function, and do exactly what I was looking for, but it is pretty messy. (I'm happy to pm you a link)

Are you saying that I could get rid of everything after the first (.*)? Some of the old references to jump.php have different requests, (company-detail, job detail, etc), just trying to clean up references to a bunch of junk that was removed, but retain function for any sites that may be linking.

racerx8413



 
Msg#: 4558390 posted 10:33 pm on Mar 25, 2013 (gmt 0)

er... redirect.. still doing it :(

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4558390 posted 12:16 am on Mar 26, 2013 (gmt 0)

welcome to WebmasterWorld, racerx8413!

what is "it" "doing"?
- post your current code
- describe what you requested
- describe what you expected
- describe the actual response

as mention by lucy24 and g1smd, everything starting from the '?' will fail the condition in the pattern, since this is not part of the path and the pattern in the RewriteRule only matches the path, not the query string.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4558390 posted 1:29 am on Mar 26, 2013 (gmt 0)

...and even when you do look at the query string:

jump.php?dbID=(.*)&ban_www=/company-detail.php?com_id=(.*)$

:: pause to count on fingers ::

I make that two ? question marks ? in the URL, which is one more than your average php script is prepared to deal with. Is the second one a literal question mark within the "ban_www" parameter so you've got nested queries and you need to un-nest them?

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4558390 posted 11:18 am on Mar 26, 2013 (gmt 0)

I make that two ? question marks ? in the URL, which is one more than your average php script is prepared to deal with. Is the second one a literal question mark within the "ban_www" parameter so you've got nested queries and you need to un-nest them?


actually the "2nd url", which is a parameter value within the query string, contains reserved characters which must be percent-encoded in the requested url such as in the (assuming HTML) href attribute value.

http://www.ietf.org/rfc/rfc1738.txt [ietf.org]:
Many URL schemes reserve certain characters for a special meaning: their appearance in the scheme-specific part of the URL has a designated semantics.
If the character corresponding to an octet is reserved in a scheme, the octet must be encoded.
The characters ";", "/", "?", ":", "@", "=" and "&" are the characters which may be reserved for special meaning within a scheme.


the TestString regex in your RewriteCond will have to look for the percent-encoded reserved characters within the QUERY_STRING.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved