using proxy instead of redirection with htaccess rewriteRule - apache

I'm developing a webapp and for the static files I'm simply using apache at localhost while the backend is on a couchdb instance running at localhost:5984.
The webapp interacts with files from the backend all the time. So what is happening when trying to test on apache all file requests to localhost:5984 are getting blocked due the cross-domain policy so the only way to get that working is starting the browser by setting flags to ignore that.
But again I get stuck when trying to test the app on mobile such ipad or iphone.
Currently I have this on my .htaccess file.
RewriteEngine on
# these are 302 http redirections instead of serving as a proxy
RewriteRule auth http://localhost:5984/auth [L]
RewriteRule db/([\s\S]+) http://localhost:5984/db/$1 [L]
RewriteRule send/([\s\S]+) http://localhost:5984/send/$1 [L]
# these are just redirections to static files and work great
RewriteRule ^([a-z/.]+) _attachments/$1 [L]
RewriteRule ^$ _attachments/ [L]
As you can see I have really no idea on how to deal with apache configuration unfortunately.
But what is happening right now is that for some of these rules apache is simply redirecting the page instead of provide it as a proxy server which causes the issue with cross-domain.
Also on the first auth rule I send POST and DELETE requests which as a redirection instead of proxy it won't pass the data being POSTed through.
So what I would like to achieve is to activate some kind of feature (if it exists) which will make apache simply render the page as it was on the localhost domain instead of redirect it. (I named this a a proxy, but perhaps that's not even the right term, sorry for any mistake committed with the nomenclatures).
Is is possible to achieve such action?
Thanks in advance

Have a look at these links / options:
[P] flag:
http://httpd.apache.org/docs/current/rewrite/flags.html#flag_p
http://httpd.apache.org/docs/current/rewrite/proxy.html
mod_proxy (possibly -- but I think #1 should be enough if it's on the same server):
http://httpd.apache.org/docs/current/mod/mod_proxy.htm

Related

.htaccess redirects if the condition doe not match/ negative condition

I am modifying the .htaccess file of a legacy PHP web application. I am not familiar with apache .htaccess syntax. I found this tutorial. What I am trying to do is that I am trying to redirect all the requests to a URL/ path if the request URL is not a specific URL/ path. For example, all the requests to the website will be redirected to localhost/my-custom-page unless the request URL is localhost/my-custom-page.
I know how to redirect mapping 1 to 1 as follows:
RewriteEngine on
RewriteRule ^my-old-url.html$ /my-new-url.html [R=301,L]
But, what I am trying to do is that redirecting all the requests to the specific page unless the request is to that page. Even the home page will be redirected to that page. How can I do that?
When I tried the following solution
RewriteEngine on
RewriteCond %{REQUEST_URI} !/my-new-url\.html
RewriteRule ^ /my-new-url.html [R=301]
I get the error
I want to check using OR condition as well. For example, if the path is not path-one or path-two, redirect all the requests to path-one.
Your question is a bit vague, due to your wording. But I assume this is what you are actually looking for:
RewriteEngine on
RewriteCond %{REQUEST_URI} !/my-new-url\.html
RewriteRule ^ /my-new-url.html [R=301]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
RewriteCond %{REQUEST_URI} !/my-new-url\.html
RewriteRule ^ /my-new-url.html [R=301]
There are a few potential issues with this, particularly since you hint in a comment that you are perhaps using a front-controller to "route" the URL.
This redirect satisfies the conditions outlined in the question, but does assume that you have no other rewrites, have an essentially "static site" and are not linking to any static resources.
You are missing an L (last) flag, so processing will continue through the file and possibly be rewritten if you have later rewrites.
If you are rewriting the URL to a front-controller in order to route the URL (as you suggest in comments) then this redirect will break, as it will redirect away from the front-controller. You need to only redirect direct requests, ie. when the REDIRECT_STATUS environment variable is empty.
If you are linking to any static resources in the same file space then these will also be redirected. You need to create an exception for any static resources you are using, either by file extension (eg. (css|js|jpg|png)) or by location (eg. /static).
So, try the following instead:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} !\.(js|css|jpg|png)$
RewriteRule !^my-custom-url$ /my-custom-url [R=302,L]
You don't need a separate condition to implement the exception for the URL you are redirecting to. It is more efficient to do this directly in the RewriteRule pattern.
The first condition ensures we are only redirecting direct requests and not rewritten requests to your front-controller.
The second condition avoids any static resources also being redirected. You could alternatively check the filesystem path if all your resources are stored under a common root. Or, as a last resort, implement filesystem checks (ie. RewriteCond %{REQUEST_FILENAME} !-f) if your static resources are too varied - but note that this is less efficient.
You will need to clear your browser cache before testing, since any earlier (erroneous) 301s are cached persistently by the browser.

how to stop to use ssl on tuleap 9.1?

On centos 6, tuleap 9.1, after installation I am only able to access the main page on http, the rest is not available because each links are root on https. Is there's a way to deactivate ssl completely?
I installed all, now can access to the first presentation page, but only if I use http and not https. Problem all the rest of the link of page ( create account, connexion etc...) redirect to https. I already try to deactivate https without success.
Can anyone can help to disable https and is stopping using ssl definitely can generate issue when using this tool?
You could force your website to only load on HTTP through your .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ http://www.{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]
I've included www in the Rewrite, if you don't want that then you can remove that section. I've also set R=302so that it is a temporary redirect. Set this to R=301 once you know it is working, as that will make it permanent.
Make sure you clear your cache before you test this.

Apache mod_rewrite

I'm using Liferay 6.2 EE that runs on tomcat but it's fronted by an Apache server. I want to redirect users so that whenever they hit the old liferay URL, it redirects them to the new liferay URL. I changed the URL in liferay, so it is now the new URL. However, whenever I try to go to the old URL, I get a page request error. It never redirects me to the new URL. In /san/apache/conf/ I put my redirect code inside of httpd.conf. This my code:
RewriteEngine On
RewriteRule ^group/old/(.*) /group/new/$1 [L]
After I applied these changes, I restarted the Apache server and it still doesn't work. I've tried a bunch of other combinations as well. Does anyone know what I'm doing wrong? Is there some place else I have to make this change?
Ah, since your rewrite rule is lying in the server config file (instead of htaccess file), the mod-rewrite receives URLs with the leading slashes (/). So, the rule should be:
RewriteEngine On
RewriteRule ^/group/old/(.*) /group/new/$1 [L]

Apache - redirect only the domain URL to a subpage

I have a shared hosting Apache server, and I'm trying to send visitors who come to the main domain URL to a specific page, with the URL replaced and a 303 redirect:
example.com
example.com/
to
example.com/subdirectory/page.html
Only the plain domain URL should get redirected, not:
example.com/page.html
example.com/otherdirectory
example.com/otherdirectory/
example.com/subdirectory
example.com/subdirectory/
example.com/subdirectory/otherpage.html
example.com/subdirectory/otherdirectory
example.com/subdirectory/otherdirectory/
example.com/subdirectory/otherdirectory/page.html
I'm not sure that RewriteEngine is allowed on this server, if there are alternative approaches possible.
The crude way I've thought of is to just use DirectoryIndex to send the visitor to example.com/index.php -- and have a PHP redirect in that file go where I want. But I'm not sure if this might produce a visible blip for some visitors, or how Google would feel about it.
I've found other instances of this kind of question on Stack Overflow, but the answers are failing for me in some way or another. As the behavior is not intuitive, testing before posting might be advisable.
Thanks
If mod_rewrite is not enabled you can use mod_alias based rule like this in your DocumentRoot/.htaccess:
RedirectMatch 303 ^/?$ /subdirectory/page.html
UPDATE: Equivalent mod_rewrite rule:
RewriteEngine On
RewriteRule ^/?$ /subdirectory/page.html [L,R=303]

Rewrite URL .htaccess - Apache server

On my website, I would rename the URL on address bar, from
domain.com/economy/article.php?id=00
to
domain.com/economy/id-name-article.html
I wrote this .htaccess file:
RewriteEngine On
RewriteRule ^([0-9]+)-([^\.]*)\.html$ http://domain.com/economy/article.php?id=$1 [L]
I have an anchor with this href: href="economy/id-name-article.html" and when I click on it, the server is redirected on article.php, it runs the script in the correct way and I can view the article, but on the address bar is still written domain.com/economy/article.php?id=00 instead domain.com/economy/id-name-article.html. Why?
This happens only on my online server, while locally it's all right.
The mod_rewrite module is issuing a redirect to your browser rather than transparently rewriting the url, causing you to see the new url in your browser.
Try removing the http://domain.com portion from your RewriteRule to see if it avoids the redirect to your browser by changing the rule to:
RewriteRule ^([0-9]+)-([^\.]*)\.html$ /economy/article.php?id=$1 [L]
If that fails, you could also use the proxy flag [P] to force apache to transparently fetch the page and return it to your users without the redirect. I don't recommend this approach since it can have security implications but it should work if the above doesn't.
EDIT: To clarify, rewriting the url with a fully-qualified domain rather than a relative uri tells apache that the redirect is on a different server, and therefore it doesn't know that the new url is accessible on the same host without redirecting the client.