vanity url and 404 - .htaccess

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

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 disable visiting .php and .html and redirect to 404

I have this code in my HTACCESS
ErrorDocument 404 /errors-404.html
ErrorDocument 403 /errors-404.html
RewriteEngine On
RewriteBase /admin/
DirectoryIndex /auth/login.php
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.php - [F,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9]+)(/)?$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
I want visitor to redirect to my custom 404 page when they visit www.site.com/login.php it must be www.site.com/login to get to login page but the problem in my HTACCESS is my DirectoryIndex when they open my site they redirect to my 404 page

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]

.htaccess rewrite url showing 404 not found

i want to change this URL
domain.com/adsbycat/sub_category.php?category=car-and-buses&category_id=1&sub_category=Car
to
domain.com/car-and-buses/1/Car
i write this code to do this
RewriteEngine On
RewriteBase /domain.com/
RewriteRule ^adsbycat/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ adsbycat/sub_category.php?category=$1&category_id=$2&sub_category=$3
but when i request for new URL it showing 404 not found. please fix this code.
Have this rule in site root .htaccess to rewrite domain.com/car-and-buses/1/Car:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/[^/]+/(.+)$ adsbycat/sub_category.php?category=$1&category_id=$2&sub_category=$3 [NC,L,QSA]

Multiple 404 pages with 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]

Resources