.htaccess rules error in my website - .htaccess

I am using following .htaccess rule in my localhost :
.htaccess file :
ErrorDocument 404 /not-found
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
AddHandler cgi-script .php .php3 .php4 .phtml .pl .py .jsp .asp .htm .shtml .sh .cgi
Options -ExecCGI
so that, url is showing something like that :
www.mysite.com/about
Now, when I upload all files with .htaccess file to my web address it's showing following error message :
Forbidden
You don't have permission to access /team-building.php on this server.
Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.
How can I solve this error and You see that I am using not-found error document rules in .htaccess file. But it's not showing me the error message page if the url is wrong.
Note : For testing purpose I am using sub-domain:
sub domain : http://www.demo.mysite.com
and all current files is in public_html/demo/ folder.

Related

URL changes when using ErrorDocument 403 in .htaccess

I have written a .htaccess file to make sure that visitors are always redirected to https and to www. In addition to that I have also added a custom html page for 404 errors.
When visitors try to access a forbidden file I want them to see my custom 404 message, as to not reveal that the path contains a forbidden file.
Here is the problem. When writing for example "example-domain.com/.htaccess" (no www or https) in the browser, the URL in the address field in the browser changes to "https://www.example-domain.com/missing.html". But I want it to say "https://www.example-domain.com/.htaccess" while displaying my 404 page.
It works for 404 errors. But when typing in a path in the address field which both triggers the 403 error and fulfill at least one of the rewrite conditions in my .htaccess file (missing https and/or www) I experience the above described problem.
Here is the code in the .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^example-domain.com [NC]
RewriteRule ^(.*)$ https://www.example-domain.com/$1 [L,R=301,NC]
ErrorDocument 403 /missing.html
ErrorDocument 404 /missing.html
Best regards

Redirect from a directory to a subdirectory

I have a webserver with an the followinf structure :
www
---diceroller
------web
I would like to redirect any request incoming to /diceroller to /diceroller/web, so the users don't have to type the /web in URL.
At the root of my server is a .htaccess containing the following elements :
Options -Indexes
ErrorDocument 404 /permalien.php
AddType text/cache-manifest .appcache
I tried to use the following .htaccess placed in the diceroller forlder, but I hit a 403 :
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^diceroller/(.*)$ /diceroller/web/$1 [R=301,NC,L]
Could someone help me to figure this out ?
RewriteRule's pattern is relative to your current directory. So if your htaccess is located in /diceroller , you have to remove that from the pattern, try the following instead
RewriteRule ^((?!web).*)$ /diceroller/web/$1 [L,R]

Directory based error page redirection

I have my website with two localised content namely EN and FR and the content will be rendered through the URLs: http://www.example.com/en/ and http://www.example.com/fr/
I want to implement different error pages for each of these locale but do not I do not have an idea on how to implement through the redirect/virtual host configuration.
I read about different .htaccess file for each of the directory but that did not work.
And help will be good. Thanks in advance.
Having different .htaccess works very well, e.g.
/-+- en -- .htaccess
|
+- fr -- .htaccess
And in /en/.htaccess, you have
ErrorDocument 404 /path/to/en/error404.html
and the same with /fr/.htaccess
ErrorDocument 404 /path/to/fr/error404.html
You can also put error documents in the same directory and differentiate by extension, e.g. error404.en.htaml and error404.fr.html. You have then
ErrorDocument 404 /path/to/error404.en.html
and
ErrorDocument 404 /path/to/error404.fr.html
respectively.
If you don't want to use .htaccess files, you can achieve the same with Directory or Location directives. In your main or virtual host configuration, this would look like
<Directory "/path/to/example.com/en">
ErrorDocument 404 /path/to/error404.en.html
</Directory>
<Directory "/path/to/example.com/fr">
ErrorDocument 404 /path/to/error404.fr.html
</Directory>
The <Location> directive looks similar.
On Apache 2.4 you can use expressions:
# for /fr/ URIs
<If "%{REQUEST_URI} =~ m#^/fr/#">
ErrorDocument 404 /fr-404.html
</If>
# for /en/ URIs
<If "%{REQUEST_URI} =~ m#^/en/#">
ErrorDocument 404 /en-404.html
</If>
Check Apache 2.4 expression reference.
If you're still stuck with Apache 2.2 then you can use:
RewriteEngine On
# for /fr/ URIs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^fr/ /fr-404.html [L]
# for /en/ URIs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^en/ /en-404.html [L]
However keep in mind that Apache 2.2 solution will return 200 status to clients.

.htaccess help - not working

I want to rewrite all .php into .html,,, so i created a .htaccess file and added
AddHandler application/x-httpd-php .php .html .htm
but when it seems not working...
here i uploaded all files - http://www.fellowindian.com/ca/index.php & http://www.fellowindian.com/ca/page1.php
1: Do you have mod_mime installed on Apache.
2: Are you sure that .htaccess is executing.
Simple test would be to see if it can rewrite your urls to add / remove www from it.
Example:
Options +FollowSymLinks RewriteEngine
On RewriteBase / RewriteCond
%{HTTP_HOST} !^www\.mycee\.com$ [NC]
RewriteRule ^(.*)$
http://www.mycee.com/$1 [R=301,L]
3: Which user is the owner of the .htaccess file and what is its attributes?
4: Check that the AllowOverride directive is set in your apache config and not set to None.
Test by putting invalid directives in the .htaccess file and reloading the page.
If the apache error log doesn't show any errors, it's not executing.
5: If you're on shared hosting, check with your host if they have AllowOverride enabled or not.
Personally, I think the best place to put the AddType directive would be in apache's httpd.conf as .htaccess puts a performance hit on your server, but in the case of shared hosting, .htaccess is usually the only option available.
If you're trying to redirect something.php to something.html, you could do
RewriteEngine On
RewriteCond %{REQUEST_URI} .php$
RewriteRule (.*?).php $1.html

Rewrite path before processing directory?

I'm having a small problem with my htaccess files. Currently, it redirects everything back to index.php for processing, except when someone tries to access an actual directory. When the directory exists, it displays the 403 error page instead of rewriting the path to index.php like it's supposed too. What can I modify to make it always go to index.php when the files are accessed via the internet, but still load the correct files in PHP on the server?
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
ErrorDocument 403 /index.php?403
ErrorDocument 404 /index.php?404
ErrorDocument 414 /index.php?414
RewriteCond %{HTTP_HOST} !^$ [NC]
RewriteRule !^(.*) index.php [L]
Sample file structure:
Web Directory
- com
- source
- index.php
- TEST.HTML
The folders such as 'com' and source' will display the 403 because the user doesn't have access to them. The files such as 'index.php' and 'TEST.HTML' execute normally. I want my htaccess to redirect everything in the Web Directory folder back to the index.php file, no matter what.
I think you want this instead:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
ErrorDocument 403 /index.php?403
ErrorDocument 404 /index.php?404
ErrorDocument 414 /index.php?414
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule .* index.php [L]
This was on the assumption that you didn't want to be able to access TEST.HTML directly and didn't want to change the URL in the user's browser. If either of those assumptions were wrong, let me know and I'll update the answer with the appropriate rewrite information.

Resources