RewriteCond to skip actual directories seems to be ignored - .htaccess

My problem is that The RewriteRule is still matching when I visit a physical directory, e.g. http://a-domain.com/foo/ where foo is a normal directory in the web root.
the .htaccess file has:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .? index.php [L]
It works fine on my dev server but not on a live WHM/CPanel server. I'm a bit lost.

I've tested this on my cpanel website and you're right and I found out where's the problem. when you try to access a folder in your website that doesn't have default file (index) , it tries to access the file that is responsible for 403 HTTP code, and because that doesn't exist, it rewrites URL to index.php.
All you have to do is add this to above of your .htaccess file:
ErrorDocument 403 /index.php?type=err&code=403

I tried this on a VM which mirrors my own shared service and it works fine for me. Have you got another .htaccess in the foo directory? If so, then the rewrite engine will ignore your one in DOCROOT and use this instead?

The /foo/ dir has basic authentication so it was failing when %{REQUEST_FILENAME} contained a non-existent file /home/the_user/public_html/401.shtml. I didn't think this was an issue because the basic authentication worked, and when I cancelled the auth prompt I was being served a standard 401 file. Usually when those ErrorDocument files are missing, Apache says Additionally, an error of type 404 was encountered while trying to use an ErrorDocument to handle the request.

Related

seturlstrategy- page reload not working :Flutter web 2.0

I have used setUrlStrategy(PathUrlStrategy()); to remove # from the url, but since then my pages stopped reloading after I deploy my project on web, it shows 404 page not found. but works perfectly on localhost.
if I remove setUrlStrategy(PathUrlStrategy()); and build again to deploy on web it works. but that # shows on url.
Can anyone help?
p.s: I used my project on non root location, and have already added my project name in index file, my project works if I open first page to next file, but not directly when open next file from url.
ok i found the problem, it was because i didn't use .htaccess file.
here is htaccess code if anyone get same problem.
RewriteEngine On
RewriteBase /myproject/
RewriteRule ^index\.html$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /myproject/index.html [L]
</IfModule>
put this file where your index file is.

removed .html extensions with htaccess now index.html give 403 error

After entering the code below, my home page gives a 403 error. The rest of the site works perfectly. All instances of .html were removed.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Any advice?
Thank you!
example.com leads ti the 403 error. If I write example.com/index it works fine.
Something else must have changed for this to result in a 403 error. The code you posted won't actually do anything when you request example.com/ - the same as if that code didn't exist at all. (UPDATE: However, this assumes your .htaccess file is located in the document - it appears this is not the case - see below.)
However, what will trigger a 403 in such cases is when "formatted directory listings" are disabled and the directory index document cannot be found (or has been disabled).
So, try setting the appropriate directory index at the top of your .htaccess file:
DirectoryIndex index.html
It is the DirectoryIndex that serves the appropriate file when requesting your "home page", not your directives in .htaccess.
UPDATE:
It [.htaccess] is located in my root directory. Would it be better to put it in the public_html folder?
Yes, the code you posted should go in the /public_html directory (ie. your document root). If these directives are in a .htaccess file above the document root then the RewriteRule pattern will match the URL-path public_html/ and rewrite the URL to public_html/.html which is possibly where your 403 error is coming from ("dot" files are usually hidden/protected OS files and you may also have a directive in your server config blocking access. However, this behaviour may also be dependent on other factors in the server config/OS). However, with that code in the document root then a request for example.com/ (your home page) won't be processed by these directives (which is good) - mod_dir should then serve the index.html file in this instance.
However, you don't want to process "directories" anyway (public_html is obviously a "directory", not a file). Which is what's happening above. eg. .html shouldn't be appended to public_html/ to begin with (or example.com/path/to/directory/ or any other directory). This can be avoided by adding an additional condition to your rule block to avoid directories (as well as files). For example:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.html [L]
Simply adding that additional RewriteCond directive might be enough and still allow you to keep your .htaccess file above the document root. (However, you may still need to move the .htaccess file as well, as described above.)
Also, the NC flag is not required here and literal dots don't need to be escaped when used inside a character class.
You could also extend this code to first check the existence of the file (with a .html extension) before rewriting, although this may be unnecessary in your case. For example:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^.]+)$ $1.html [L]
This requires an additional "file check" which may be an unnecessary overhead.

404 redirect using .htaccess

I want to set up a rule in my .htaccess file so that any url that is enetered, that results in a 404 because there is no such file, automatically re-directs to the home page of the site:
index.php
my .htaccess file looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^queenslandbeerweek.com.au$ [NC]
RewriteRule ^(.*)$ http://queenslandbeerweek.com.au/$1 [L,R=301]
ErrorDocument 404 /index.php
This causes the index.php file to show but is broken and leaves the eroneous URL in the address bar.
I have read in the answer to another post that it has something to do with passing the erroneous URL as a parameter, causing the page to not load properly, because the page calls data from a database and it is passing the bad URL as a parameter of index.php but there was no hint as to what the solution is.
What I would like to happen, is if an incorrect URL is typed into the address bar, or if a link is followed, to a file that does not exist, the completely forget about this file, drop everything, and go to the home page index.php.
index.php calls data from a database
Is this possible using a .htaccess file?
I have exactly the same problem with another of my sites.
Any help would be greatly appreciated.
Cheers, Al.
I dont think you can directly redirect an error document but you can catch nonexistent files and folders
!-f means not a file !-d means not a directory, $1 is whatever is in (.*) (the path in the url)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php?errorurl=$1 [R=301,L]
ErrorDocument 404 /404.php
You can place the 404 error template anywhere you want. For example you could place all error messages in a folder called errormessages
ErrorDocument 404 /errormessages/404.php

empty HTACCESS drops webpage (403 error)

I know there are a few of (probably) the same questions out there, but I just can't find any working solution.
I've placed the .htaccess file next to my index.php on the server.
And even if the .htaccess is empty the site returns error 403 (forbidden access).
What I wanted to do is remove the .php extension.
http://foo.net/web_new/index_1024.php
And even if I put the following into the .htaccess, the website still returns error 403.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>
I've found on the web, to enable the mod_rewrite, I should enable it in httpd.conf. But I search the whole server (ftp), but there is no clue.
where phpinfo() finds conf.d file
/etc/php5/conf.d
where is my root
/srv/www/web6/
Any idea, what could i do?
Make sure that .htaccess has 644 permissions
I think you can remove that ifModule... I find it "messy" there...
Your code looks well...

How can I get non existant files mapped correctly in .htaccess?

Duplicate:
How to rewrite non existant files to
‘default’ files?
(.htaccess)
How would I "rewrite" to a location if a file doesn't exist? I don't want to use a 404 redirect, but an actual rewrite.
So for example, let's say it is a directory with images. If the image isn't found, then it rewrites to a default image?
I.e.,
images/1.jpg
images/2.jpg
images/default.jpg
if someone tried to access "website.com/images/3.jpg",
since that doesn't exist, I want it to go to:
"website.com/images/default.jpg"
This was a previous "posted" solution, but didn't quite work:
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule /images/.* /images/error.jpg [L]
It still doesn't "get" the right image (just goes as a regular 404 request).
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^images/.* /images/error.jpg [L]
Obviously this only redirects if missing file is under /images/... but you can easily modify it for your own needs
Well, your previous posted solution is on the right track, but there's some slight craziness with it. Try this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule images/.* /images/default.jpg [L]
You should better send a 404 status code if the file really doesn’t exist rather than just a substitute with a status code other than 404. Otherwise the URL will be handled as valid.
So in your case I recommend you to set the ErrorDocument of the images directory to your default image:
<Directory "/path/to/your/images/">
ErrorDocument 404 /images/default.jpg
</Directory>
But note that the <Directory> block is only available in the server or virtual host configuration context and not the per-directory context (thus .htaccess).
If you cannot use the above, you could use a custom script as your custom error document to check what URL has been requested (see Request_URI environment variable) and send the default image if necessary. The ErrorDocument directive then should look like this:
ErrorDocument 404 /your-error-404.script
re-write your 404 document for your images folder:
(In your .htaccess file in your images folder)
ErrorDocument 404 default.jpg

Resources