enabling RewriteEngine in apache (uniserver) causes 403 on all files - apache

What I'm trying to do is to enable a 'dry' version of url rewriting in Apache only to check if it is working and actually do not rewrite anything yet.
The simplest .htaccess that I came up with is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
But this still doesn't work. All files, all paths I'm trying to request, no matter if they exist or not return 403 Forbidden
What I need is any example htaccess that has redirection inside that must work, that is allow access to all existing files and folders and do it's job only when the file requested doesn't exit.

The problem was that I've not added these lines:
Options +SymLinksIfOwnerMatch
or
Options +FollowSymlinks

Related

Problems redirecting from a 403 using .htaccess (Yourls)

I'm using a php app called Yourls. It's a self-hosted url shortener and it's pretty great, I'm happy with its overall functionality. Due to the nature of its development however there isn't much in the way of support. Let's pretend the base url is af.to, where a shortened url would be af.to/goo that might redirect to whatever url is defined by 'goo'. The problem I'm facing is that if someone goes to af.to, they end up on a 403-Forbidden. I'd rather the client is redirected to a specific url instead. I have already picked up a plugin for Yourls which redirects to a url when a shortlink is not found or mis-typed, but this does not cover the base of af.to
I attempted to put in a 403 redirect in the .htaccess, but that broke the whole yourls script resulting in a 500 server error.
Current .htaccess looks like this:
# BEGIN YOURLS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /yourls-loader.php [L]
</IfModule>
# END YOURLS
Any help on what I need to do?
Thank you.
The RewriteCond blocks tell the RewriteRule to skip existing files / folders. When you go to http://af.to/, the root folder exists : no redirection. The apache server doesn't find any index.html (or index.php) file, isn't allowed to list the content of the folder, give up and returns a 403 Forbidden.
You can create the index.html file to show some content or you can add these lines to redirect to an other url :
# just after RewriteEngine On
RewriteRule ^/$ http://my-compagny.com/ [L,R=301]

.htaccess error 500 or access forbidden

Here is my .htaccess:
<Files.htaccess>
order allow,deny
</Files>
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(\w+)$ ./redirect.php?url_token=$1
My script's logic:
My script is not in the root of the website, it's in a folder (ex: www.mywebsite.com/script/). I have the .htaccess file in this folder.
The PHP script gets that url_token value and processes it, it is working well, i've tested it.
But when I use this .htaccess file and I try to access an URL like www.mywebsite.com/script/fa34d where fa34d is a random generated code, I get:
Error 500 on XAMPP and Access Forbidden on another online host.
What can be the problem?
I've already spent more time with this than the rest of the script.
You're likely creating a loop or you're redirecting to a file outside of your document root. If the folder accessed as the root is that same as what displays as www.mywebsite.com then you can set RewriteBase / in your access file. Remember all .htaccess files work together and also work with your VirtualHosts directives on the server.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)$ /redirect.php?url_token=$1
You just need to add this line to hide your directory from http request in your .htaccess file
Options -Indexes
Enjoy!!

HtAccess Rewrite Can't Find File

I have a small web app that I'm in the process of deploying to a server. However, suddenly the htaccess file isn't working (not the index file, but when I go to a url such as /login). It keeps maintaining that the file doesn't exist, however, the file that it displays as "unfindable" definitely does exist. I checked the relative path /home/sites/xxx.co.uk/public_html/index.php displayed by the error with a file_get_contents and it shows the file.
DirectoryIndex index.php
Options -Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*[^/])/?$ index.php?route=$1 [L,QSA]
Does anyone know what is happening?
Sorted it, it was just something with my Hosting Provider. The site was being tested on a test url they provided (while the domain is registered) and they had a strange method of identifying your folder (needed /home/sites to be removed)

Apache ignoring file extensions

I use the following .htaccess code to make my URLs cleaner:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
It basically check if the requested URL points to a file or a directory and if it doesn't, it formats it in a particular way.
The problem is that my production server seems to ignore file extensions when it checks if the requested URL points to a file. For example, it would consider the URL /contact pointing to a file named contact.jpg if a file with that name existed on the root of the server.
What causes Apache to behave like that and what can I do to control it - make it strict about file extensions?
I believe it's because of MultiViews option.
Try Options -MultiViews in the .htaccess

How would I go about creating a mod_rewrite that redirects to launch.php?i=/the/url/that/they/want?

So if the user types mydomain.com/dashboard, the document the server actually sends them is /launch.php?i=/dashboard.
The one caveat is that I would like to leave requests for
/flags
/people
/posters
/css
/icons
/images
/libraries
/patterns
alone, and they should request the actual folder.
How would I create such a mod_rewrite?
This is the .htaccess file for the CakePHP Framework.
Please replace the index.php and ?url= to fit your needs.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
The "!-d" tells Apache to follow existing folders and "!-f" to follow existing files.
Everything else is channelled through index.php
As suggested in a comment, you have to be aware that if it's not working it could be because mod_rewrite is not enabled and you'll not get an error stating that fact, you'll probably only have a HTTP 404.