htaccess block direct access to specific file extensions with exceptions - .htaccess

I am trying to prevent users from accessing any files or directory directly except from my index.php using this htaccess:
Options -Indexes
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(www\.)?REMOVEDLINK [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?REMOVEDLINK.*$ [NC]
RewriteRule \.(gif|jpg|png|js|css)$ - [F]
ErrorDocument 403 http://REMOVEDLINK/404.html
ErrorDocument 404 http://REMOVEDLINK/404.html
I just used another htaccess within my php includes folder:
deny from all
Everything works fine except for the fact that it prevents my favicon from being displayed. Code from my index.php:
<link rel="shortcut icon" href="img/favicon.png" type="image/x-icon" />
How can I allow access for the favicon to be displayed?

Just below RewriteEngine On line include this rule to allow access to favicon:
RewriteRule img/favicon\.png$ - [NC,L]

Related

Redirecting to another page - htaccess

I'd like to redirect the user to a page named test.php when he types the url of my site (example www.domain.com).
Pratically by typing www.domain.com the browser should show www.domain.com/test.php
At index.html or index.php, you could add meta refresh tag like this :
<meta http-equiv="refresh" content="5;www.domain.com/test.php" />
If you use index.php, the syntax would be like this:
echo '<meta http-equiv="refresh" content="5;www.domain.com/test.php" />';
Add http:// at the url if You want to redirect to another domain.
In top of your .htaccess file try this rules (except files and directories rewrite anything www/non www to example.com/test.php)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule (.*) http://example.com/test.php [L]
</IfModule>

htaccess 404 page for specific subdirectory only (static html pages)

Stuck again with htaccess. How can I add a 404 page to a specific subdirectory?
In my case the 404 page should only affect the "staging" subdirectory (and the directories inside of staging) but not the root or any other subdirectories placed in root.
http://example.com/staging
The code which is in my .htaccess file for the directory "staging":
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
Options -Indexes -MultiViews
RewriteEngine on
RewriteRule ^arbeiten/$ /staging/arbeiten/alle [R=301,L]
RewriteRule ^journal/$ /staging/journal/journal-alle [R=301,L]
ErrorDocument 404 /staging/404.html
Using the code above (and many others) results in a Internal Server Error.
Update #1 to my initial question.
In general, the code/answer provided to add a 404 not found page to a specific sub directory works:
ErrorDocument 404 /staging/404.html
The reason why it still does not work is because there must be some error in the overall .htaccess setup related to the code remove .html extension and/or redirect to a page in a folder.
Options -Indexes -MultiViews
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
RewriteRule ^arbeiten/$ /staging/arbeiten/alle [R=301,L]
RewriteRule ^journal/$ /staging/journal/journal-alle [R=301,L]
ErrorDocument 404 /at/404.html
I will continue to further investigate the setup in my .htaccess.
Update #2
Using the .htaccess script below allows to remove .html extensions, redirect pages to a specific page within a directory and basically a working 404 not found page within a specific sub directory on a Hostgator cPanel grid hosting (apache).
Options -Indexes -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]
RewriteRule ^arbeiten/$ /staging/arbeiten/alle [R=301,L]
RewriteRule ^journal/$ /staging/journal/journal-alle [R=301,L]
ErrorDocument 404 http://www.example.com/staging/404
Remaining 404 problem:
http://example.com/staging/blabla (404 works)
http://example.com/staging/folder/blala (404 works)
http://example.com/staging/folder/project-detail-pagexxx (404 works)
http://example.com/staging/folder/project-detail-pagexxx/ (404 does not work)

Specific url redirect using .htaccess

I have this domain "www.mydomain.com/great/redirect"
I want to redirect it to "www.redirect.com/great/redirect"
using .htaccess so how can I do that?
Right now I am doing that using html
<meta http-equiv="refresh" content="0;url=http://redirect.com/great/redirect" />
in location of "www.mydomain.com/great/redirect" file.
Inside site root .htaccess of www.mydomain.com you can use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^great/redirect/?$ http://redirect.com%{REQUEST_URI} [L,NC,R=301]

404 for simple redirect to /filename.html

I want to redirect to a certain html file in root when the user enters the domain.com. Simple as that. Get an 404.
RewriteRule .* /intro.html [NC,L]
Any ideas?
I have tried this in already:
<base href="/">
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^$ /intro.html [L]

Redirect domain to 404 generic page

I have my website (a.com). Another person is pointing your domain (b.com) to my server. That is wrong.
Is it possible to redirect all traffic that comes from b.com for a generic 404 page? think this is possible with a file htaccess
Thanks you!
Edit
i can do this?
RewriteEngine On
RewriteRule ^(.*)$ http://www.b.com/$1 [R=404,L]
or
RewriteCond %{HTTP_HOST} ^www.b.com [nocase]
RedirectMatch 404 ^(.*)
You could do something like this:
UPDATE
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^domainb\.com$ [NC]
RewriteRule .* - [R=404]
You could also try a general forbidden error, like this:
order allow,deny
deny from b.com
allow from all
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_REFERER} b\.com [NC]
RewriteRule ^ /404.html [R=404,L]
This will check if traffic is coming from b.com and if yes then it will redirect all the requests to a generic 404.html page.
How I did it was to put this code in the htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js)$
RewriteRule .* /index.html [L,R=302]
</IfModule>
Which send the request to the index.html file. Then use the refresh function in html to redirect the file to the desired domain:
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<META http-equiv="refresh" content="0;URL=https://www.mydomain.co.uk/">
</head>
<body>
</body>
</html>

Resources