Welcome to WebmasterWorld Guest from 3.81.29.226

Forum Moderators: Ocean10000 & phranque

Apache rewrite rule for navigation to dynamic URL

Apache rewrite rule

     
11:11 am on Oct 4, 2019 (gmt 0)

New User

joined:Oct 4, 2019
posts: 5
votes: 1


I have reached here after trying to look around for a solution to my Apache rewrite redirect rule. So basically I have the following rewrite requirement

From
"https://<mydomain>/projects/XYZ?selectedItem=release-page"

To
"https://<mydomain>/plugins/servlet/project-config/XYZ/versions"

So if you see the second URL where I basically need to have the value 'XYZ' from the first URL dynamically captured. So the bit highlighted in bold would be different for different requests. Hope that's clear what I am after.

Please can I have some suggestions?

Thanks
2:31 pm on Oct 4, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3153
votes: 7


Can you be more specific as to what XYZ is? Are there restrictions to letters/numbers/case/other characters? Or can it literally be "anything"? Generally, you want to be as specific as possible.

Are you wanting an external "redirect" or an internal "rewrite"?
6:43 pm on Oct 4, 2019 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15934
votes: 887


Are you wanting an external "redirect" or an internal "rewrite"?
Or possibly even both, if you picture an old URL structure with a query string, being redirected to a new prettier URL without the query--but the content is still getting served from the version with the query via an internal rewrite.

Except, in that case, would you really want the /plugins/servlet/ element to be part of every URL?

Is the query string always exactly "selectedItem=release-page" ? Can other parameters occur, and if so, what do you want done with them?

For the domain name, use example.com (or example.anything if you need to cite more than one).

The best way to ask your question is something like this: “When visitors see ABC in their address bar, I want to serve content from DEF without letting them know I’m doing it”. Or, in the alternative, “When visitors request ABC, I want them to be redirected to DEF which will be visible in their address bar”.
9:57 pm on Oct 4, 2019 (gmt 0)

Administrator

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

joined:Aug 10, 2004
posts:11870
votes: 244


welcome to WebmasterWorld [webmasterworld.com], VenkateshAynala!

we'd like to see your best attempt at writing the directives you need.
you'll get more helpful responses that way.

from the Apache Web Server forum Charter [webmasterworld.com]:
It is not appropriate to expect other members to write your code for you or to debug your entire project; Please don't expect other members to solve a problem you don't want to begin solving yourself.

and
"Fix my code" and "Do my homework for me" threads:

This is a discussion forum, not a help desk or a free code-writing service; If you have a problem, please try to research it and then phrase your post in a manner conducive to general discussion of the issue. Rather than providing one-off solutions, we prefer to help people find resources to help themselves.

A general guideline for code-related problems is: Post your own code and describe what you hoped it would accomplish. Then describe how it fails and include all relevant information from your server error logs.
...
7:20 am on Oct 5, 2019 (gmt 0)

New User

joined:Oct 4, 2019
posts: 5
votes: 1


I probably should have been lot more clear. Sorry about that. So in the from URL https://www.example.com/projects/XYZ?selectedItem=release-page, everything remains same except 'XYZ'. Also there's no restriction and it can literally be "anything". Further its an internal rewrite in this case.

The URL to which it should redirect to also would remain same always as this https://www.example.com/plugins/servlet/project-config/XYZ/versions, just that as I mentioned whatever is the string value for 'XYZ' in the first URL it replaces the 'XYZ' in the second one.

I have experience working with redirect rules where you just redirect to a static URL but here in this case as you see the 'To' URL needs to have a part of the string dynamically set. Hence I am afraid I have no idea on how to go about that and have tried to get help from different places on web but with no clues.

Thanks
3:37 pm on Oct 5, 2019 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15934
votes: 887


Well, you need to capture the XYZ element (in parentheses) so you can reuse it in the target as $1. And in order to do that, you need to figure out the pattern. But if XYZ is the last thing in the URL, it really doesn’t much matter: everything after /projects/ gets captured.
Further its an internal rewrite in this case.

The URL to which it should redirect to
It can’t be both. Either it’s an internal rewrite:
RewriteRule (blahblah) $1 [L]
or it’s an external redirect:
RewriteRule (blahblah) $1 [R=301,L]
or possibly
RedirectMatch 301 (blahblah) $1

What are some things you’ve tried that didn’t work? If you look through old threads in this subforum, you’ll find that quite often we do end up writing your code for you--even though we loudly assert that we won’t--but only after we’ve seen evidence that you really are trying to get a grip on it. It's so you don't start out asking how to do ABCD, and then come back next week asking how to do ABCE, which exasperates everyone.
9:07 am on Oct 15, 2019 (gmt 0)

New User

joined:Oct 4, 2019
posts: 5
votes: 1


Apologies for coming back late on this. So this is the rewrite rule I have managed to come up with

RewriteRule ^projects/([a-z]+) https://www.example.com/plugins/servlet/project-config/$1/versions? [NC]

which means we can now redirect from https://www.example.com/projects/XYZ?selectedItem=release-page to https://www.example.com/plugins/servlet/project-config/XYZ/versions

However I have URLs like the following as well which are getting picked by above rule as well

https://www.example.com/projects/XYZ/issues
https://www.example.com/projects/XYZ?selectedItem=components-page

How do I modify my above rule so that it does not apply above two URLs but only to the one highlighted in bold

Thanks
9:46 am on Oct 15, 2019 (gmt 0)

New User

joined:Oct 4, 2019
posts: 5
votes: 1


Please ignore my last comment. I have managed to work it out by adding a RewriteCond.

Thanks for all the help here.

Cheers
9:59 am on Oct 15, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3153
votes: 7


Please include your working example, it could help other readers.
5:52 pm on Oct 15, 2019 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15934
votes: 887


I have managed to work it out by adding a RewriteCond.
Make sure it has to be a RewriteCond, and can't be embodied in the RewriteRule itself. If it involves looking at the query--like your "components-page" vs. "release-page"--then yes, it has to be a condition. But anything involving the URLpath--like "blahblah/ABC" vs. "blahblah/ABC/more-stuff-here" can and should go in the body of the rule.
8:51 am on Oct 16, 2019 (gmt 0)

New User

joined:Oct 4, 2019
posts: 5
votes: 1


Here's the rule for your reference

RewriteCond %{QUERY_STRING} ^selectedItem=release-page
RewriteRule ^projects/(.+) /plugins/servlet/project-config/$1/versions? [R,L]
5:14 pm on Oct 16, 2019 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15934
votes: 887


RewriteCond %{QUERY_STRING} ^selectedItem=release-page
Does the "selectedItem" parameter always come first in the query string? If you can be certain it does, then the ^ is appropriate because it lets the server get out of there all the faster. But if it can also occur non-initially, you should replace the ^ with a \b (word boundary anchor, which would match either ^ or & ).