I have a project at hand but encountered an error.
I have RewriteRule to redirect index.php?view=$1 to /. However, when I accessed a URL that is supposed to lead to a 404 Error, it displays the home page(index.php) instead.
ErrorDocument 404 /pages/errors/error_redirect.php
ErrorDocument 500 /pages/errors/error_redirect.php
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
<FilesMatch "\.tpl$">
Order Allow,Deny
Deny from all
</FilesMatch>
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /pages/errors/404.php [L]
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /pages/errors/500.php [L]
RewriteCond %{REQUEST_FILENAME} !-l
# Rewrite all URLs to non-extension URLs
RewriteRule ^(admin|user)($|/) - [L]
RewriteRule ^([A-Za-z0-9-_]+)$ /index.php?view=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
The problem I am facing is that if I entered http://localhost/asdasdads, the browser led me to http://localhost/ instead with unchanged URL in the browser but browser displaying index.php. It should have shown a 404 Error page.
I believe RewriteRule ^([A-Za-z0-9-_]+)$ /index.php?view=$1 [L] is the root of this problem. It is redirecting /index.php?view=asdasdads to /index.php?view= due to it is not found. I would want it to redirect to 404 instead.
Thank you in advance.
Aizat, try removing the unnecessary rewrite conditions, and give a try on this:
The simplest way to set a 404 error page is by directly setting a 404 error message in the .htaccess file itself:
ErrorDocument 404 /404.php
Related
I created a 404.php page and added the following in my .htaccess file
ErrorDocument 404 /404.php
Also played around with removing the slash, adding a full URL, etc,
No matter what I do, I get my index.php UI regardless to what I write in the URL. Here is the thing, IT IS NOT re-directing me to domain.com/index.php or "/". The URL remains, but the UI is the index.php content
I'm adding my re-direct below in case you see something there that is conflicting
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
From what I know of the mod_rewrite the last 4 lines are saying
That if the request filename is not a file nor a directory,
redirect to index.php.
And this is what is happening I believe.
All of the routes that are not available on the physical path in your application are going to index.php.
You will need some 404 mechanism in your application. If you are using some framework it usually has an exception like "RouteNotFound" thrown and then error handler redirects to a 404 page, you can also do something similar and redirect from within your application to 404.php.
if($should_be_an_error) {
header("Location: 404.php");
}
(haven't used something like this for years but should be something similar)
or remove
`
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
`
But then your other routes might stop working.
EDIT:
Since you wanted to redirect to 404.php instead of index.php(getting that from comments) the code should be changed to
RewriteRule . /404.php [L]
(last line of the rewrite block)
You can do this using FallbackResource directive. Remove all the code starting with RewriteBase / line and use this single line:
FallbackResource /404.php
This will fallback to /404.php for any 404 event.
You can see this code below as an example if it match your requirement:
ErrorDocument 404 http://example.com/404/
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /404.php [L]
Hi am a bit slow when it comes to htaccess and this has been giving me a headache all day. I am not sure what I am doing wrong but all my combos of attempts to fix this go awry.
I want index.php to be set as the default index page for folders and the homepage of the site to redirect to formacompany.com/en I don't want .php on the end of my files.
Here is my code i want www.example.com/ru to display the index.php file in the directory but don't want the url to be www.example.com/ru/index
DirectoryIndex index.php
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
redirect 301 /admin/ http://www.example.com/_admin/index.php
redirect 301 /payment https://www.example.com/en/secure/ccpayment
redirect 301 http://www.formacompany.com http://www.formacompany.com/en/index
when i visit example.com/ru i get this as the url and a page error:
http://www.example.com/ru/index//index//index//index//index//index//index//index//index//index//index//index//index//index//index//index//index//index//index//index//
Don't mix mod_rewrite rules with mod_alias ones and keep redirect rules before internal rewrite ones:
DirectoryIndex index.php
ErrorDocument 404 /404.php
RewriteEngine On
RewriteRule ^admin/?$ http://www.example.com/_admin/index.php [L,R=302]
RewriteRule ^payment/?$ https://www.example.com/en/secure/ccpayment [L,R=302]
RewriteRule ^/?$ http://www.formacompany.com/en/index [L,R=302]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
I created custom 404 error page called error.php, now I want to display the error.php content in user entered url.like this link: http://www.youtube.com/asdfasfsd
This is my htaccess code:
ErrorDocument 404 https ://localhost/path/error.php
I want to show the error.php content in same URL without redirect to error.php page
if user typed invalid url (for example:https ://localhost/path/nnn.php)
current result:redirecting to error.php
expected result:display error.php content in https
://localhost/path/nnn.php
My full htaccess code:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule index.html$ index.php [L]
RewriteRule service.html$ service.php [L]
RewriteRule ^([^/]*)\.html$ about.php?pid=$1 [L]
RewriteRule ^cont/([^/]*)\.html$ contact-inner.php?tid=$1 [L]
RewriteRule ^contact/([^/]*)/([^/]*)\.html$ contact-page.php?tid=$1&ona=$2 [L]
RewriteRule ^about/([^/]*)\.html$ about-inner.php?oid=$1 [L]
RewriteRule ^service/([^/]*)/([^/]*)\.html$ service-page.php?oid=$1&ona=$2 [L]
ErrorDocument 404 https://localhost/ezhil/path/public_html/error.php
<Files 403.shtml>
order allow,deny
allow from all
</Files>
deny from 117.202.102.84
deny from 58.68.25.210
deny from 91.200.13.112
deny from 86.128.130.170
deny from 91.200.13.7
deny from 173.208.206.90
Please remove your ErrorDocument rule and replace it with following code :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ error.php [L]
A suggestion by the way: You should put a [L] behind RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} , so that no other rule will override the HTTPS-enforcement.
Your .htaccess will then look like this:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
RewriteRule index.html$ index.php [L]
RewriteRule service.html$ service.php [L]
RewriteRule ^([^/]*)\.html$ about.php?pid=$1 [L]
RewriteRule ^cont/([^/]*)\.html$ contact-inner.php?tid=$1 [L]
RewriteRule ^contact/([^/]*)/([^/]*)\.html$ contact-page.php?tid=$1&ona=$2 [L]
RewriteRule ^about/([^/]*)\.html$ about-inner.php?oid=$1 [L]
RewriteRule ^service/([^/]*)/([^/]*)\.html$ service-page.php?oid=$1&ona=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ error.php [L]
<Files 403.shtml>
order allow,deny
allow from all
</Files>
deny from 117.202.102.84
deny from 58.68.25.210
deny from 91.200.13.112
deny from 86.128.130.170
deny from 91.200.13.7
deny from 173.208.206.90
Modify the line in your .htaccess file. Just use path and filename (Without host)
ErrorDocument 404 /path/error.php
It worked for me.
You may need to tweak your existing htaccess a bit for this to work
RewriteEngine On
# your existing rules. please note the [L] after each rule
RewriteRule ^main$ index.php [L]
RewriteRule ^about-us$ aboutus.php [L]
RewriteRule ^whatever$ whatever.php [L]
# anything else that ends with .php
RewriteRule .*\.php$ error.php
as soon a RewriteRule is met, further execution is stopped by [L]. Then if the request ends with .php the error.php will be executed.
Just add code like this in your .htaccess
ErrorDocument 404 "<script>document.write(atob('[Your Html code convert to base64string]'))</script>"
For Example
ErrorDocument 404 "<script>document.write(atob('PCFET0NUWVBFIGh0bWw+CjxoZWFkPgoJPG1ldGEgY2hhcnNldD0idXRmLTgiPgoJPG1ldGEgaHR0cC1lcXVpdj0iWC1VQS1Db21wYXRpYmxlIiBjb250ZW50PSJ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anMiPjwvc2NyaXB0PgoJCSAgPHNjcmlwdCBzcmM9Imh0dHBzOi8vb3NzLm1heGNkbi5jb20vcmVzcG9uZC8xLjQuMi9yZXNwb25kLm1pbi5qcyI+PC9zY3JpcHQ+CgkJPCFbZW5kaWZdLS0+Cgo8L2hlYWQ+Cgo8Ym9keT4KCgk8ZGl2IGlkPSJub3Rmb3VuZCI+CgkJPGRpdiBjbGFzcz0ibm90Zm91bmQiPgoJCQk8ZGl2IGNsYXNzPSJub3Rmb3VuZC00MDQiPgoJCQkJPGgxPjQwNDwvaDE+CgkJCTwvZGl2PgoJCQk8aDI+T3BzcyEgVGhlIHJlcXVlc3RlZCBVUkwgd2FzIG5vdCBmb3VuZCBvbiB0aGlzIHNlcnZlci48L2gyPgoJCQk8cD5UaGF0J3MgYWxsIHdlIGtub3cuPC9wPgoJCQk8YSBocmVmPSIjIj5HbyBUbyBIb21lcGFnZTwvYT4KCQk8L2Rpdj4KCTwvZGl2PgoKPC9ib2R5Pgo8L2h0bWw+Cg=='))</script>"
It's worked for me.
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]
hi have this file htaccess, i need to improve a 404 page, now when you visit a page that not exist return the index.php content and the url doesn't change, i would like to put a custom 404 , i tried with "ErrorDocument 404" but doesn't work.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^ricercadilavoro.it
RewriteRule ^(.*)$ http://www.ricercadilavoro.it/$1 [R=permanent,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{REQUEST_URI} index\.php$
RewriteRule ^(.*)index\.php$ /$1/ [R=301,L]
rewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ $1/ [NC,R=301,L]
</IfModule>
Using the ErrorDocument directive is the correct way to do this. Make sure you're using a path relative to the server root.
ErrorDocument 404 /404.html
For more information, read this http://www.apache.com/docs/httpd-docs-2.2.13.fr/mod/core.html#errordocument.