Multiple 404 pages with htaccess - .htaccess

I have a htaccess that checks the IP and then redirects the user to the appropriate page. Right now I'm using ErrorDocument 404 /404.php but I want to have seperate 404 pages based on the IP. If I place that under the..
RewriteCond %{REMOTE_ADDR} x.x.x.x
rule it still goes to the same 404 page. How do I make the 404 page account for the IP like the rest of the pages do?

You cannot put conditions before ErrorDocument directive.
As a workaround you can use mod_rewrite to handle this situation for you:
RewriteEngine On
# handle 404 for IP=x.x.x.x
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REMOTE_ADDR} =x.x.x.x
RewriteRule . /ip1-404.php [L]
# handle 404 for IP=y.y.y.y
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REMOTE_ADDR} =y.y.y.y
RewriteRule . /ip2-404.php [L]

Related

htaccess send 404 status on multiple 404 redirects

I have the following rules in my .htaccess file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/en-us/
Rewriterule ^(.*) /en-us/error-404/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Rewriterule ^(.*) /error-404/ [L]
And it's working fine. When there are requests starts with /en-us/, I can see the content of /en-us/error-404/ page and in all other cases I see the content of /error-404/ page. However, the status code received from the server is 200 OK.
Is it possible to send 404 Not Found in both cases?
You can use if/elseif/else conditions to use different ErrorDocument directives: (available in Apache 2.4+)
<If "%{REQUEST_URI} =~ m#^/en-us/#">
ErrorDocument 404 /en-us/error-404/
</If>
<Else>
ErrorDocument 404 /error-404/
</Else>

How to make the settings of custom 403 page and url rewriting coexist?

In my .htaccess file, I have the settings for rewriting urls such as http://www.myownwebsite.com/about.php to http://www.myownwebsite.com/about/.
And I also have the settings for custom error 403 and 404 pages.
The issue I'm encountering is that, when I'm testing accessing a folder directly, for example, http://www.myownwebsite.com/css/, instead of seeing the custom 403 page, I'm seeing the custom 404 page. I guess that's because the settings of url rewriting is overwriting the setting of custom 403 page.
Here is the related code in my .htaccess file:
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
What am I doing wrong?
Before adding .php to your URIs make sure corresponding .php file exists.
Better to keep redirect rule before other rules.
Have it like this:
Options -MultiViews -Indexes
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/$ $1.php [L]
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php -f
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php [L]

Show 404 page with redirecting to www only

I want to change the 404 error. I redirected the 404 error page to a custom one, without redirecting the URL itself. This works, see (http://www.gincher.net/not.exist).
I'm using Google PageSpeed, which works only when the user enters from www. So I add a redirect to add a www, which works for me.
The problem starts when I combine them (http://gincher.net/not.exist). It gives this error:
Moved Permanently
The document has moved here (gincher.net/not.exist).
Additionally, a 500 Internal Server Error error was encountered while
trying to use an ErrorDocument to handle the request.
Here is my HTACCESS code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^gincher.net$
RewriteRule (.*) http://www.gincher.net$1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /error.php [L]
Please advice!
First of all, you're redefining the behaviour of ErrorDocument.
Your rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /error.php [L]
is the same as
ErrorDocument 404 /error.php
Then, you still have a syntax error in your first rule.
Replace your current code by this one
ErrorDocument 404 /error.php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^gincher\.net$ [NC]
RewriteRule ^(.*)$ http://www.gincher.net/$1 [R=301,L]
Note: you also have the possibility to write the same rule without hard-coding your domain name
ErrorDocument 404 /error.php
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

why doesnt my htaccess allow for multiple rules

I was wondering why my htaccess would not allow for multiple rules? Here is the code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [QSA,L]
Basically I wanted to add a 404 rule. Here is the code:
ErrorDocument 404 http://xxxxxx/404.php
Well basically I can not add all of the together in one htaccess file. Here is the code:
ErrorDocument 404 http://xxxxxx/404.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [QSA,L]
That code above will not work all together. Why is that?
It's not working because basically you are doing two times the same thing in the .htaccess:
ErrorDocument 404 http://xxxxxx/404.php
The ErrorDocument redirects to 404 in case the requested page is not found. The two RewriteConds RewriteCond %{REQUEST_FILENAME} !-f and RewriteCond %{REQUEST_FILENAME} !-d plus the RewriteRule will do exactly the same, they will redirect if the request file was not found - in your case they are redirecting to index.php.
So you can try either of the two following:
RewriteEngine on
ErrorDocument 404 /404.php
or
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ 404.php [QSA,L]
Currently the URL is being treated as a string, to let it know it's a path you should add a slash at the beginning
try this and see if it works
ErrorDocument 404 /404.php

vanity url and 404

I need vanity urls, how can i make them redirect to the custom 404 page if they are not found in my DB?
.htaccess code
ErrorDocument 404 /url/notfound.php
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(\w+)$ code.php?code=$1

Resources