Folder Not Raising 404 Error In htaccess - php

Simply, I am trying to make it so if a user goes to a folder, it will give a 404 Error. Problem is, it does not seem to work!
Here is my file setup:
- postin
- mail
- yourmail.php
- profiles
- viewprofile.php
- .htaccess
- error.php
Then simply I am adding this to my .htaccess:
RewriteRule ^mail/ - [L,R=404]
ErrorDocument 404 error.php
I thought by adding this code that if a user typed in the URL .../postin/mail/ that it would take the user to the 404 error.php page. That does not seem the be the case though, it will just bring up the directory page for .../postin/mail/. So my question is, how do I raise the 404 error when a user goes to a folder (directory)?
If you would like my full .htaccess it is here:
Options +FollowSymLinks
RewriteEngine on
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule search/(.+)/?$ search.php?query=$1 [NC,L]
RewriteRule search/?$ search.php [NC,L]
RewriteRule register/?$ registerpage.php [NC,L]
RewriteRule login/?$ loginpage.php [NC,L]
RewriteRule home/?$ home.php [NC,L]
RewriteRule users/?$ users.php [NC,L]
RewriteRule error/?$ error.php [NC,L]
RewriteRule ^mail/ - [L,R=404]
ErrorDocument 400 http://localhost/postin/error
ErrorDocument 401 http://localhost/postin/error
ErrorDocument 403 http://localhost/postin/error
ErrorDocument 404 http://localhost/postin/error
ErrorDocument 500 http://localhost/postin/error
ErrorDocument 502 http://localhost/postin/error
ErrorDocument 504 http://localhost/postin/error
<Files .htaccess>
order allow,deny
deny from all
</Files>

You can use :
DirectoryIndex /errorpage.php
This should be the ver first line in your htaccess

Related

RewriteRule in .htaccess for directories destroys general 404 ErrorDocument and creates 500

I added a RewriteRule to make the path of '/some-page' opening the '/some-page/index.php' but showing '/some-page/' in the browser's address bar. This works fine as expected.
On the other hand I have now the problem that whenever I type a not-existing path like e.g. '/this-does-not-exist.html' I am prompted a 500 message and not redirected to the 404 page anymore.
And if I type e.g. '/kontakt' I am prompted with an 404. But if I type '/kontakt.php' it works. It should show '/kontakt.php' if I type '/kontakt' and should show '/kontakt' in the browser's address bar.
Here is the part of my .htacces:
ErrorDocument 400 /400.php
ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php
ErrorDocument 503 /503.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]

404 error page not redirected to custom page using htaccess

My htaccess rule to redirect 404 error to custom page isn't working.
I put the below code:
<IfModule security2_module>
ErrorDocument 404 https://example.com/404.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule index\.html$ index.php [L]
</IfModule>
And I used both - ErrorDocument 404 https://example.com/404.php & ErrorDocument 404 /404.php
Pls let me know if anyone have solution
ErrorDocument 404 http://jobguide.australianenglishcenter.com/404/
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ <YourRelativePathToPHPFile>/404.php [L]
Here,The ErrorDocument redirects all 404s to a specific URL
The Rewrite rules map that URL to your actual 404.php script. The RewriteCond regular expressions can be made more generic if you want, but I think you have to explicitly define all ErrorDocument codes you want to override.

Htaccess RewriteRule redirects to 404 page

My basic requirement is:
Remove .php extensions from all the urls.
Rewrite the urls from http://localhost/unsync/softwares/page_name/sub_category/ to http://localhost/unsync/softwares.php?p=page_name&sub_cat=sub_category
The following is my .htaccess code:
# Do not remove this line, otherwise mod_rewrite rules will stop working
Options +MultiViews
RewriteEngine On
RewriteBase /
#Prevent viewing of .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
#Prevent directory listings
Options All -Indexes
#Error Documents
ErrorDocument 400 /unsync/error.php?code=400
ErrorDocument 401 /unsync/error.php?code=401
ErrorDocument 402 /unsync/error.php?code=402
ErrorDocument 403 /unsync/error.php?code=403
ErrorDocument 404 /unsync/error.php?code=404
ErrorDocument 500 /unsync/error.php?code=500
ErrorDocument 503 /unsync/error.php?code=503
#Remove extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ /unsync/$1.php [NC,L]
RewriteRule softwares/(.*)/(.*)/$ /softwares.php?p=$1&sub_cat=$2 [L]
DirectoryIndex index.php
The problem I am facing is that, RewriteRule fails. I mean, when I try to access softwares/page_name/sub_category, its throwing a 404 error.
Note: Its removing the .php extensions properly and working fine with normal pages.
The problem is that your rewrite rule rewrites all requests to /unsync/request.php, you have to check the existance of php file before rewriting the request,
#Remove extensions
RewriteCond %{DOCUMENT_ROOT}/unsync/$1.php -f
RewriteRule ^([^\.]+)$ /unsync/$1.php [NC,L]
Or you can simply exclude the slash in pattern so that it can not conflict with your other rule
RewriteRule ^([^\/.]+)$ /unsync/$1.php [NC,L]
Rewrite rules are tried in order.
This means softwares/page_name/sub_category is rewritten by the first rule to /unsync/softwares/page_name/sub_category.php, which is not found. Therefore, you get a 404 Not found error.
After a long day of research, I could finally resolve my issue on my own as follow (if anyone facing similar issue is searching for solution):
#If both p and sub_cat are issued
RewriteRule ^softwares/(.+)/(.*)$ /unsync/softwares.php?p=$1&sub_cat=$2 [NC,L]
#If only p is issued
RewriteRule ^softwares/(.*)$ /unsync/softwares.php?p=$1 [NC,L]
#Remove extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ /unsync/$1.php [NC,L]
DirectoryIndex index.php

Custom error page failed to load after editing htaccess

I edited my htaccess file to rewrite mysite.com/page to mysite.com/page.php and to show 404 error page if a user requested mysite.com/page.php instead of mysite.com/page
I also added my own custom 404 error page
But when I requested mysite.com/page.php, the error page shown was Apache default 404 error page (instead of my custom error page), with additional error info:
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
My error log:
Request exceeded the limit of 10 internal redirects due to probable configuration error.
Here is my htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]
ErrorDocument 404 /error/404
I have tried changing 404 at the last line above to 404.php but the custom error page still not showing up..
However, the custom error page did show up when I requested mysite.com/page/ (notice the slash).
I don't understand, where this error comes from. But you can resolve it by exiting the rule chain when 404.php is requested. Insert this rule at the beginning
RewriteRule ^error/404.php$ - [L]
Unrelated, but you can simplify your rules a little bit
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_FILENAME}.php [L]
RewriteCond %{THE_REQUEST} \.php
RewriteRule \.php$ - [L,R=404]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php
ErrorDocument 404 /error/404.php

mod_rewrite issue with parameters

I have the following .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^portfolio-single/([^/.]+)/?$ portfolio-single.php?id=$1 [L]
<Files ~ "^\.(htaccess|htpasswd)$">
deny from all
order deny,allow
</Files>
Options -Indexes
ErrorDocument 400 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 405 /error.php
ErrorDocument 408 /error.php
ErrorDocument 500 /error.php
ErrorDocument 502 /error.php
ErrorDocument 504 /error.php
Currently all pages that end with the php extension are working fine. So for example www.somewebsite.com/blog.php is now accessable via /blog. However I want a certain page, namely, portfolio-single.php?id=123 to be accessible via /portfolio-single/123. The 4th line you can see code that I took from this website, and I have been trying to get it to work but keep getting a 404 when I access portfolio-single via /portfolio-single/123. What am I missing? A follow up question is if I have to reboot XAMPP for changes to the .htaccess file to take effect (so far it has worked without).
Try swapping the two rules. The last rule is more specific so it should match first.
While your current approach works, it's more common to use one catch-all rule and a generic handle script which does the routing.
RewriteRule ^(.*)$ handler.php?path=$1 [L]
Then handler.php can decide what to do with blog and portfolio-single/x and non-existing-page.
The router can now be implemented in PHP which gives you more freedom, like dynamically adding routes and matches based on more than a regexp.
Another advantage of this approach is that your application now has a single point of entry. This makes bootstrapping much easier since there is only one path.
For adding .php extension you must check whether corresponding .php file exists firts:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [NC,L]
RewriteRule ^portfolio-single/([^/.]+)/?$ portfolio-single.php?id=$1 [L,NC,QSA]

Categories