.htaccess internal rewrite with preserving query string - .htaccess

i need to rewrite a url, preserve one query string for internal rewrite.
and another for display.
so this current url:
www.mysite.com/staff/teachers?id=37:john
needs to be internally rewritten to:
www.mysite.com/staff/teachers/37
not quite sure where to start, im trying this, but don't know how to access the number between "id=" and ":" to use for the rewrite
thanks for your help.

You cannot rewrite a URL "for display". All you can do is tell the browser to load a new URL. When the browser requests the new URL, the server has no way of knowing why it is being loaded (i.e. that it was the result of a previous redirect) so that new URL will need to include enough information to load the actual page.
In other words, you have to either:
include the ID in the "pretty" URL, such as www.mysite.com/staff/teachers/37-john
have your application look up the user based on their name in the internally rewritten URL, e.g. www.mysite.com/staff/teachers?lookup_name=john

Related

RewriteRule - redirect multi variable URL to multi variable URL

Our old website has a search URL structure like this:
example.com/Country/United States/Region/California/Area/Southern California/City/San Diego/Suburb/South Park/Type/House/Bedrooms/4/Bathrooms/3/
This is currently rewritten to point to the physical page:
/search/index.aspx
The parameters in the URL can be mixed up in different orders, and the URL can include one or more parameters.
We want to 301 redirect these old URLs to a new structure that is ordered in a logical way and more concise:
example.com/united-states/california/southern-california/san-diego/south-park/?type=house&bedrooms=4&bathrooms=3
example.com/united-states/california/?type=house&bedrooms=4&bathrooms=3
Is there a way with URL rewriting to interrogate the old URL, work out what parameters are existing and then write out the new URL structure?
Even if we can limit it to just the Country, Region, Area, City and Suburb, that may be good enough to at least return some results even if it's not perfect.
Also, spaces should be turned into hyphens and all text made lowercase.
I already have the RewriteRule to turn the new URL structure into a URL to point to a physical page. It's just transforming the old URL in to the new URL I need help with. I've googled endlessly and it's just beyond me!
Can anyone help? Thanks.
Since you already have the old search page with rewriting rules set up for it and which is capable of parsing all parameters you need, the easiest and most appropriate solution I see here is to issue a redirect you require from this old search page's code. Just put the code that composes new URL with all parameters needed and redirects from this page - this should be a lot easier than trying to parse all these parameters in .htaccess and combine them into the new format.

URL rewriting with OcpSoft rewrite Join.path rule

I'm using Ocpsoft Rewrite, to perform URL rewriting in a JSF project. I have a redirect rule, which works fine:
.addRule()
.when(Direction.isInbound().and(Path.matches("/venue/{id}")))
.perform(Redirect.temporary(context.getContextPath() +
"/protected/index.xhtml?venueID={id}"));
However, because of the redirect, this changes the URL in the navigation bar. I thought I could use a Join rule instead, but it doesn't work as i expected:
.addRule(Join.path("/venue/{venueID}").to("/protected/index.xhtml"))
.perform(Log.message(Level.INFO, "Rewrite is active!"));
I thought this rule would redirect from, for example, foo/venue/123 to foo/protected/index.xhtml?venueID=123, but I don't get the ?venueID=... parameter appended to the URL.
Anyone knows what the correct rule should look like?
Your rule looks correct. But a Join doesn't result in a redirect. Instead it forwards the request internally. This means that the URL isn't changed. So you won't "see" the parameter venueID in the URL. But you will be able to read the parameter using the standard Servlet API:
String id = HttpServletRequest.getParameter("venueID");
If you really want, you can do a Forward instead:
.addRule()
.when(Direction.isInbound().and(Path.matches("/venue/{id}")))
.perform(Forward.to("/protected/index.xhtml?venueID={id}"));
But this won't handle outbound HTML link correction like Join will!

301 redirect URL with query string, removing part of the beginning URL, keeping the full query at the end

I'm trying to find a redirect that will remove part of a URL (in the middle), but leaves the query string in place at the end.
I can do this fine via a single url redirect, but there are hundreds of these urls so I'm trying to find a rule that might be able to do for all of them in one fell swoop, so i don't have to make one for each and any new ones will get redirected automatically.
I'm trying to remove 'search.php' from the urls, here is an example:
www.site.com/products/search.php?rPage=/items/listing/detail_handler.jsp?code=219592&units=Feet&item_id=2624472
to redirect to:
www.site.com/products/?rPage=/items/listing/detail_handler.jsp?code=219592&units=Feet&item_id=2624472
Thanks for your time.
According to the documentation, the query string is passed through unchanged by default
RewriteRule products/search.php http://www.site.com/products/ [L,R=301]

IIS URL Rewrite - need help rewriting friendly urls for a forum

I'm new to using the URL Rewrite module and I'm having trouble with what I thought would be a simple URL rewrite for forum threads (using IIS 7.5)
I need to rewrite:
/forum/100/2534/friendly-title
or:
/forum/100/2534/334/comment/friendly-thread-title
to:
/forum/?forum=100&thread=2534&post=334&postType=comment
The rule that I have written (not working) is:
^forum/([1-9][0-9][0-9]*)/([1-9]*)/(([1-9]*)/(post|comment)/)?([a-zA-Z0-9-]{5,50})$
Which maps to:
/forum/?forum={R:1}&thread={R:2}&post={R:4}&postType={R:5}
I'm getting a 404 error.
It's correct that {R:4} and {R:5} are empty when you use the first URL. That's because there are no values for these fields. The RegEx still matches though so the URL will still be rewritten. Your code should properly handle empty values for the post and postType querystring parameters to display the entire thread and not just a specific comment (at least that what I assume is suppose to happen).
By the way, a more logical URL structure would be:
/forum/100/2534/friendly-thread-title/comment/334
This won't help you this this particular problem though but just on a side note.

rewrite just the url in .htaccess and not change parameters passed to script with get

Is it possible to rewrite the URL part of a request but still pass the original request as a get parameter to php.
For example, my search url might be www.mysite.com/search/Search%20Request when the search request has been entered by the user and is multiple words, lower and upper case etc, I want to rewrite the url to all lowercase with no enocding signs etc, so the url is www.mysite.com/search/search-request but my get parameter gets passed correctly as &search=Search%20Request.
How can I rewrite just the url in .htaccess and not change the parameters I pass to my script with get.

Resources