Error 404 script not working, if - php

My error404 script was working perfectly, but then I did this:
I had non-friendly URL's like this:
localhost/website/user.php?name=UserA
Then, I updated my .htaccess
#Options +FollowSymlinks
RewriteEngine On
RewriteRule ^([0-9A-Za-z-]+)/?$ user.php?name=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
ErrorDocument 404 /website/error404.html
After this, my URL's are friendly URL like this: localhost/website/UserA
But, the error404 script do not work When the page is something like this: localhost/website/dsfdsfsfasfsfsfs
But if the page is something like this: localhost/website/SomeThingHere/sdfsfs

Use:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9A-Z-]+)/?$ user.php?name=$1 [NC,L]
ErrorDocument 404 /website/error404.html
With RewriteCond before RewriteRule
But you rewrite /website/dsfdsfsfasfsfsfs to user.php?name=dsfdsfsfasfsfsfs
You need to change user.php to show 404 error when the user does not exist

Related

Page not redirecting if not exists, just shows code with no data from other page

So I have an issue where I'm not being redirected to the 404 if the url doesn't actually exist, instead, it shows code from another page.
So I have a page called viewpost.php which typically works only if like viewpost.php?id=slug-text-of-post, but if you were to say go to bloggg.php, which doesn't exist, it would show you the viewpost page code as if you viewed it without any GET variables. So, the URL will show bloggg.php, but show the code from viewpost.php as if nothing would have been passed to it and shows this
empty page with no data to fill it
This below is my .htaccess
RewriteEngine On
ErrorDocument 404 http://www.example.com/404.php
RewriteBase /
RewriteRule ^c-(.*)$ viewcat.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
Best way to achive what you want:
RewriteEngine On
ErrorDocument 404 http://www.example.com/404.php
RewriteBase /
RewriteRule ^cart/([0-9]*)$ viewcat.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^post/([0-9]*)$ viewpost.php?id=$1 [QSA,L]

Clean Url Error 404

Just wanted to customize Error 404 in my htaccess. The problem is when the user goes to Error 404 page. The URL is not clean compares to others. URL looks like these index.php?page=page-not-found .. How can I make it http://example.com/page-not-found.htm or http://example.com/page-not-found/ ??? By the way, have a look on my codes.
Options All -Indexes
RewriteEngine On
ErrorDocument 404 http://example.com/index.php?page=page-not-found
RewriteRule ^([^/]*)\.htm$ /website/index.php?page=$1 [L,QSA]
RewriteRule ^([^/]*)/lesson/([^/]*)\.htm$ /thesis/index.php?page=$1&lesson=$2
Any idea guys? By the way, thanks..
Remove http:// from your 404 directive to avoid server making a full redirect for 404:
Options All -Indexes
RewriteEngine On
ErrorDocument 404 /index.php?page=page-not-found
RewriteRule ^([^/.]+)\.htm$ /website/index.php?page=$1 [L,QSA]
RewriteRule ^([^/]+)/lesson/([^/.]+)\.htm$ /thesis/index.php?page=$1&lesson=$2 [L,QSA]
Please try this one.First remove your extensions from all files and try this one
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^p/([0-9]+)$ /p.php?photo=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(Joe|Mao|Napoleon|Gandi)$ /profile.php?username=$1 [QSA,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)$ /$1.php [QSA,L]

htaccess two commands in the same file

Hi i have my htaccess with this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.htm [L]
ErrorDocument 404 /404.htm
if i erase the first 4 lines, the other piece of code (which redirects de 404 page when there's a page not found) does not work anymore.
Any ideas?
I also want that when it redirects to 404.htm, it does not show on the url box, this url
http://www.mypage.com/404.htm
i want to show this
http://www.mypage.com/fail/search-by-the-user
for example.
Try following .htaccess
RewriteBase /
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ /$1.html
ErrorDocument 404 /404.html
NOTE: if you are working at localhost then please mention proper RewriteBase

Mod_rewrite not redirecting properly

first of all I know there are questions about my problem I tried few of them, but unsuccessfully.
I am trying to get:
http://example.com/project/text
from
http://example.com/projects.php?c=text
I have this .htaccess:
RewriteEngine on
RewriteRule ^project/(.*)$ projects.php?c=$1 [L,QSA]
ErrorDocument 404 404.php
When I open the site i.e. http://example.com/project/123456 I get nothing(Blank site).
Thank you for you help.
You can try:
ErrorDocument 404 404.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^project/(.+)$ projects.php?c=$1 [L,QSA,NC]

How to merge to .htaccess files

This is for 404 error pages:
ErrorDocument 404 /404.php
This is for url rewrite:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /profile.php?username=$1
When i try to use them both it works but when i type in the url smth. like host/username it redirects to 404.php but this username exists.
you simply combine them together
i've tried it on index.php
and i got the index not 404 when i tried host/eli
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /profile.php?username=$1
also you should do in profile.php
if(user_not_exists($_GET['username'])){
require_once '404.php'
}

Categories