how to configure apache mod_rewrite for web services - apache

I'm new to Apache mod_rewrite but I was able to get it to work for simple URL translation for static pages accessed using the GET method.
I'm having trouble to get the URL translation to work for web services though. Basically all I want is to redirect the request from Apache to tomcat that runs under 8093 . I tried:
RewriteRule myproject/myservices/myService/(.*) http://www.localhost:8093/myproject/myservices/myService/$1
But I get a "The document has moved here" response. Any ideas would be appreciated. Thanks.

I think the problem is that your request is only rewritten, but not proxied. You could try to append [P] at the end or look into mod_proxy: http://httpd.apache.org/docs/2.0/mod/mod_proxy.html

Related

Apache configuring a redirect for 404 behavior to try with .html suffix

I'm looking for a safe way to prevent 404s with smart redirection.
http://www.domain.com/test (results in 404)
http://www.domain.com/test.html (results in page)
Is there a way I can configure Apache to try appending .html to the URL if the page is 404? So basically if I visit: http://www.domain.com/test Apache would say "this page doesn't exist, lets try adding .html suffix to it and see if that exists".
Thanks!
Instead of configuring the apache I reverted to some logic in my application layer for these edge cases.

Redirecting from example.com to www.example.com

My site uses AJAX, and it seems like I have to include the full path when I use it to access a function. This is fine, as I can code it in. The problem is, I have hardcoded http://www.example.com... but if a user has gone to http://example.com (without the www) this gives an access problem and the AJAX won't execute.
I think the easiest way to resolve this would be to make sure that if a user goes to mysite.com, they are redirected to www.example.com.
I can find solutions online, but they all involve the htaccess file, which my server doesn't support - I have to use rewrite.script instead.
How can I do this using rewrite.script, or is there an alternative way to approach this?
Thanks for the suggestions - I was directed to this: http://seo-website-designer.com/Zeus-Server-301-Redirect-Generator which created the rewrite.script file I needed.
In .htaccess found in your root document:
Redirect http://example.com/ http://www.example.com/
If there is no support for .htaccess on your server, you may have to include meta tag redirect on the head of your page. or using javascript to check the URL source then redirect it.

Restler ssl https redirection

I'm using restler to write a REST api, and since I wan't to do some basic authentication I need to redirect every request wich doesn't come from https, to https.
Does anybody know a way of doing this using the .htaccess file provided by restler or is it better to do this redirection using php headers?
Thanks in advance!
Approach that works for me is to do it from index.php (gateway) place the code below in your index.php before any other code
if($_SERVER['HTTPS']!="on")
{
$redirect= "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
header("Location:$redirect");
exit;
}

Simple modrewrite, how does a link appear?

When you use modrewrite to rewrite your urls, when does the rewrite occur. Will the user be able to see the url before rewrite, when hovering over the link? When they hover over a link will their browser display the rewritten url or the url before it was modified with modrewrite?
The rewrite is done in the server so the user will never know,
PHP also doesn't know what link its pointing to only the script file
mod_rewrite acts on the server side, meaning that apache rewrites incoming URLs before responding to the request. Any HTML links you add to a page will point to the URL you entered, as mod_rewrite doesn't modify any outgoing data.
The rewrite occurs when Apache has parsed the request - before the PHP interpreter has been started.

Apache mod_rewrite/mod_proxy - re-write last part of URI as query string?

We have a web resource that can be accessed with a URL/URL of the form:
http://[host1]:[port1]/aaa/bbb.ccc?param1=xxx&param2=yyy...
However, we are working with an external (i.e., not developed by us, so not under our control, i.e., we can't change it) client app that is attempting to access our resource with a URL that looks like:
http://[host2]/[port2]/ddd/fff/param1=xxx&param2=yyy...
In other words, the client is including the "query string" (the ?param1=xxx&param2=yyy... part) as if it's part of the URI, instead of as a proper query string.
We have a separate Apache proxy instance, and we're thinking that we could use that with some RewriteCond/RewriteRule to take the incoming requests (the ones with the query string at the end of the "URI", and without the "?") and rewrite the URI to a "proper" URI with a "proper" query string and then use that modified/re-written URI to access our resource via proxy.
We'd also like to do that without having an HTTP re-direct (e.g., 30x) going back to the client, because it appears that they may not be able to handle such a re-direct.
I've been trying various things, but I'm not that familiar with Apache mod_rewrite, so I was wondering if someone could tell me (1) if this is possible and (2) suggest what RewriteCond/RewriteRule would accomplish this?
P.S. I have gotten some progress. The following re-writes the URL correctly, but when I test, I'm seeing a 302 redirect to the re-written URL, instead of Apache just proxying immediately to the re-written URL. Is it possible to do this without the re-direct (302)?
<Location /test/users/>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(.*)/param1=
RewriteRule ^/(.*)/param1=(.*) http://192.168.0.xxx:yyyy/aaa/bbbbb.ccc?base=param1=$2
</Location>
Thanks, Jim