I'd like to ask - is there a way to auto redirect with httacces conditions when rewrite rule doesn't exists?
For example.
I have the following rules:
RewriteRule ^/index/$ index.php
RewriteRule ^/error-404/$ 404.php
ErrorDocument 404 /error-404/
When someone will try to access adress like index/something-added-by-user/ he will see 404 page, but url will be still "index/something-added-by-user/". Is there a way to automatically redirect from that url typed by user to url "error-404/"? So when he type url like above, he will see 404 page with 404 page url.
It can be done using this rule:
# not a real dir
RewriteCond %{REQUEST_FILENAME} !-d
# not a real file
RewriteCond %{REQUEST_FILENAME} !-f
# not a real link
RewriteCond %{REQUEST_FILENAME} !-l
# redirect it to 404.php
RewriteRule ^ /404.php [R,L]
Though keep in mind that browser will not get 200 status instead of 404.
Related
My htaccess 404 rule is set as follows:
ErrorDocument 404 /404.htm
but this is what happens:
Non-existent url with no extension:
https://www.example.com/bgbgbgbgbg redirects to https://www.example.com/404.htm (correct behaviour)
non-existent url with a dot:
https://www.example.com/bgbgbgbgbg. redirects to https://www.example.com/404.htm (correct behaviour)
non-existent url with extension:
https://www.example.com/bgbgbgbgbg.htm redirects to "file not found" (should redirect to https://www.example.com/404.htm)
url endng with a slash:
https://www.example.com/bgbgbgbgbg/ redirects to a weird 404 page with no css, and the urls in the browser's address bar does not change (should redirect to https://www.example.com/404.htm)
I did some research and tred implementing these lines right under ErrorDocument 404 /404.htm:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . https://www.example.com/404.htm [L]
it solves everything, BUT it redirects the homepage (https://www.example.com) to https://www.example.com/404.htm
What can I do in order to solve the homepage issue?
To redirect non existent requests to 404 error page you can simply use :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.+$ /404.htm [L,R]
my url
www.mysite.com/content
How can i redirect my site if type wrong URL like this
www.mysite/contentfasfsa(any letter)
That would redirect to original site www.mysite.com/content/ how could i do that.
You will have to edit your .htaccess file. There are two ways for doing this :
1. ErrorDocument 404 /content (Your home page)
This will redirect all the error 404 to your homepage.
2. RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ www.mysite.com/content [L]
This rewrite rule will redirect broken links to your homepage.
Try them out.
If none of my RewriteRule [L] matches, I want to redirect to a nice url /you/shall/not/pass, but show contents of /index.html.
This is what I am doing now (this is the very last rule in the file):
RewriteCond %{REQUEST_FILENAME} !-f # is not file
RewriteCond %{REQUEST_FILENAME} !-d # is not directory
RewriteCond %{ENV:REDIRECT_STATUS} !=200 # wasn't already redirected
RewriteRule .* index.html
It works fine, but keeps whatever garbage was written in the URL. I want it to be changed.
Doing this didn't work
#RewriteCond same as above
RewriteRule .* /you/shall/not/pass [R]
RewriteRule ^/you/shall/not/pass index.html
I apparentely don't understand how [R] works, whether it continues forwarding the changed url to other RewriteRules or not and what page it redirects to when the end of file is reached.
You can use ErrorDocument 404 with a rewrite rule for this:
Options +FollowSymLinks
ErrorDocument 404 http://domain.com/you/shall/not/pass
Then create a symbolic link like this:
/public_html/you/shall/not/pass -> /public_html/index.html
Replace /public_html/ with your DocumentRoot path.
So, after countless hours I made it work, somehow, I guess...
I am too tired to investigate, but I will get back later and edit if it is wrong.
ErrorDocument 404 http://domain.com/you/shall/not/pass # redirects if document does not exist
RewriteEngine on
RewriteRule ^$ /you/shall/not/pass [L,R] # redirects if request URI is empty, skips rest
RewriteRule ^you/shall/not/pass$ /index.html [L] # puts different resource to matched url
It's kinda weird that the solution is that short.. How come I didn't try that already?
So I know this may seem a little strange but I for sake of consistency, I would like all my urls to appear in this form:
http://example.com/page/
So far I have gotten the regular pages working but I cannot seem to get the error pages working properly.
If the user visits a page or directory that does not exist, I would like the browser to hard redirect to:
http://example.com/404/
This directory, however, will not actually exist. The real location of the error page will be under
/pages/errors/404.php
Also, although I do not need an exact answer for all the various errors (400, 401, 403, 404, 500), I will be applying whatever method is given to redirect all of these to their "proper" URL's
eg.
http://example.com/400/
http://example.com/500/
etc.
Any ideas?
Try this in your .htaccess:
.htaccess
ErrorDocument 404 http://example.com/404/
ErrorDocument 500 http://example.com/500/
# or map them to one error document:
# ErrorDocument 404 /pages/errors/error_redirect.php
# ErrorDocument 500 /pages/errors/error_redirect.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /pages/errors/404.php [L]
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /pages/errors/500.php [L]
# or map them to one error document:
#RewriteCond %{REQUEST_URI} ^/404/$ [OR]
#RewriteCond %{REQUEST_URI} ^/500/$
#RewriteRule ^(.*)$ /pages/errors/error_redirect.php [L]
The ErrorDocument redirects all 404s to a specific URL, all 500s to another url (replace with your domain).
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.
Local Redirect:
Change .htaccess ErrorDocument to a file that exists (must exist, or you'll get an error):
ErrorDocument 404 /pages/errors/404_redirect.php
404_redirect.php
<?php
header('Location: /404/');
exit;
?>
Redirect based on error number
Looks like you'll need to specify an ErrorDocument line in .htaccess for every error you want to redirect (see: Apache ErrorDocument and Apache Custom Error). The .htaccess example above has multiple examples in it. You can use the following as the generic redirect script to replace 404_redirect.php above.
error_redirect.php
<?php
$error_url = $_SERVER["REDIRECT_STATUS"] . '/';
$error_path = $error_url . '.php';
if ( ! file_exists($error_path)) {
// this is the default error if a specific error page is not found
$error_url = '404/';
}
header('Location: ' . $error_url);
exit;
?>
Put this code in your .htaccess file
RewriteEngine On
ErrorDocument 404 /404.php
where 404.php is the file name and placed at root. You can put full path over here.
Try adding this rule to the top of your htaccess:
RewriteEngine On
RewriteRule ^404/?$ /pages/errors/404.php [L]
Then under that (or any other rules that you have):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ http://domain.com/404/ [L,R]
In your .htaccess file , if you are using apache you can try with
Rule for Error Page - 404
ErrorDocument 404 http://www.domain.com/notFound.html
I currently have this code in my .htaccess file
RewriteCond %{QUERY_STRING} !product_id=(.*)
RewriteRule ^(.*)$ http://www.myurl.com/index.php [R=301]
This 301 redirects product URLs to my root directory based on product ID. (I didn't make it I got it from someone else)
What I need to do is keep this rule in place whilst redirecting any 404 pages in the same directory to the homepage.
You can try adding these rules before the rule that you already have:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /? [L,R=301]
This assumes the homepage you are talking about is the document root. So for example, if /blah doesn't exist and would normally result in a 404, going to http://yourdomain.com/blah would redirect to http://yourdomain.com/.