mod_rewrite works on localhost but not when upload into server - apache

I wrote a code to redirect people who ask for foo.html to the page bar.html instead.
my .htaccess file is as below
RewriteEngine on
RewriteRule ^foo.html$ bar.html
It works on localhost.But gets the following error when runs in web server.
HTTP Error 404.0 - Not Found The resource you are looking for has been
removed, had its name changed, or is temporarily unavailable.

Related

How to redirect 404 errors (and 403) to index.html with a 200 response

I am building a static website that uses JS to parse a URL in order to work out what to display.
I need every URL to actually open index.html where the JS can pull apart the path and act accordingly.
For example http://my.site/action/params will be parsed as an action with some parameters params.
Background, this will be served from AWS S3 via CloudFront using custom error redirection - and this works fine on AWS.
I am, however, trying to build a dev environment under Ubuntu running apache and want to emulate the redirection locally.
I have found a couple of pages that come close, but not quite.
This page shows how to do the redirect to a custom error page on the server housed in a file called "404". As 404 is the actual error response code, the example looks a bit confusing and I am having trouble modifying the example to point to index.html.
The example in the accepted answer suggests:
Redirect 200 /404
ErrorDocument 404 /404
which I have modified to:
Redirect 200 /index.html
ErrorDocument 404 /index.html
However this returns a standard 404 Not Found error page.
If I remove the Redirect line, leaving just the ErrorDocument line, I get the index.html page returned as required, but the https status response is still a 404 code where I need it to be a 200.
If I leave the Redirect line as per the example I actually get the same result as my modified version, so I suspect this is the line that is incorrect, but I can't figure it out.
(I'm using the Chrome Dev Tools console to see the status codes etc).
I think I have found a solution using Rewrite rules instead of error docs.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
The key I was missing in this approach seems to be not including an R=??? status response code at the end of the rewrite rule. It took me a while to find that!
As it uses mod_rewrite rather than defining error pages I assume that the mechanism is different to how CloudFront does it, but for my dev system needs it seems that the result is the same - which means I can work on the site without having to invalidate the CloudFront cache after every code change and upload.

Redirect based on rules and error type htaccess

I have been trying to piece together my .htaccess file, I have not found a lot of information about what I am trying to accomplish or if I am over thinking it.
I am using the following to disable directory browsing it works but I would like to redirect it to a custom 404 error page I have created.
# Disable Directory Browsing
Options All -Indexes
Deny access to directories or files I am using the following
####
# Deny access to certain directories that SHOULD NOT be exposed.
####
RewriteRule ^error/ - [L,R=403]
RewriteRule ^assets/ - [L,R=403]
RewriteRule ^plugins/ - [L,R=403]
RewriteRule ^libraries/ - [L,R=403]
RewriteRule ^includes/ - [L,R=403]
RewriteRule ^bootstrap.php - [L,R=404]
all of these work as it should but I want to redirect to a custom error page in my script folder
example.com/errors/
All i have found is this
ErrorDocument 404 /errors/error-404.html
but have been unsuccessful in getting any of the errors redirected to the one I created.
Without seeing the actual response you are seeing or your full .htaccess file, my guess would be that you are blocking access to the /errors directory that contains your custom error documents. The error documents themselves need to be publicly accessible (although there are tricks you can employ to block access and serve an appropriate HTTP status code).
If you block access to the error document then Apache will fallback to the default Apache response with an additional message along the lines of:
Additionally, a "4xx/5xx error" error was encountered while trying to use an ErrorDocument to handle the request.

Apache 2.4 Internal mod_rewrite is not working

I have the following excerpt in my .htaccess file:
RewriteEngine On
# Whether RewriteBase is commented does not appear to have an impact on my problem
# RewriteBase /
RewriteRule ^admin/(.*) members/$1 [L]
The idea is that the pages in /admin load from the /members directory, since certain features are duplicated between them, so that I don't have to duplicate my code. (There are other directives in the file that are related to a CMS system, and those are working as expected.)
On the current Apache 2.2 server, everything works fine. My client's webhost wants to upgrade to a server with Apache 2.4, and on the test server the rewrite rule fails. Instead of loading the page, it just displays "File not found."
All other rules are executed as expected, it's just that one that fails. I have tried adding/removing / characters and removing [L] from the rule, it always fails. I tried adding [R] to send the redirect to the browser, but even that failed. But if I change the url to the same filename but in the members directory, then the file loads successfully.
Being able to change one directory name to another seems like a pretty basic function of mod_rewrite. What am I missing?

Configure apache server to pick up the HTML5 base tag href for angularjs one page app

I read at least a dozen of posts about it, and found something like to setup in my site's root folder (which is www/pg-dev). htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^weiadesigner/pg-dev
RewriteRule ^subdir/(.*)$ http://weiadesigner/pg-dev/$1 [L,R=301]
it suppose to (I think), redirect from any attempt to access weiadesigner/pg-dev to http://weiadesigner/pg-dev/$1. However, still nothing happen, I still receive the 404 Not Found issue.
I access the WAMP server through my computer name which is weiadesigner with chrome. /pg-dev is the folder where my site is. I try to tell the Apache to redirect all subdomain access (meaning, if someone access weiadesigner/pg-dev/events in a url bar in a browser) , for example weiadesigner/pg-dev/events, will respond with AngularJS router instead of the 404 Not Found from the Apache server, because I am using the HTML base tag that is setup in the index.php in its with
<base href="/pg-dev/">
<base target="_blank" href="http://weiadesigner/pg-dev/">
I am having a big hard time to find any valid solution on this.

.htaccess rewrite to unmounted folder resource

Im trying to use a .htaccess file on an apache server to point /cmsfiles/flipbooks/thisFlipBook/ to /thisFlipBook
Ive tried:
RewriteRule ^/thisFlipBook/ /cmsfiles/flipbooks/thisFlipBook[L,R=302]
as well as:
RewriteRule ^/thisFlipBook/cmsfiles/flipbooks/thisFlipBook[L]
(without the R tag so that mod_rewrite knows its an internal apache redirect instead of having the browser send a new request for the rewritten resources.)
The first rule results in a 404, and the second results in:
/thisFlipBook/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/
However if you manually navigate to /cmsfiles/flipbooks/thisFlipBook it manages to find the index.html file and load it successfully.
Am i doing something wrong?
Also: server side apache redirects and unmounted folder are the correct way to be doing this right? Client started requesting so many flipbooks that we've moved them out of the project for deployments sake.