I have htaccess file below
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Redirect /index.html /login.html
ErrorDocument 404 /404.html
ErrorDocument 403 /403.html
Options -Indexes
Redirect /index.html /login.html
When user enters index page it should redirect to login.html page.
But it doesn't work.
Redirect /index.html /login.html
im requesting /index
If you are requesting /index then I'm not sure why you are checking for /index.html in your rule. However, you should be using mod_rewrite to construct this redirect since you are already using mod_rewrite for your internal rewrites.
So, have it like this instead:
Options -Indexes
ErrorDocument 404 /404.html
ErrorDocument 403 /403.html
RewriteEngine On
RewriteBase /
# Redirect "/index" to "/login"
RewriteRule ^index$ /login [R=302,L]
# Append ".html" if target file exists
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule (.*) $1.html [L]
I've also "fixed" your .html rewrite since this could result in a 500 error for certain requests and there's no need to check that the request does not map to a directory before checking that the request does map to a file.
See the following question on ServerFault that expands on this potential issue regarding .html removal. https://serverfault.com/questions/989333/using-apache-rewrite-rules-in-htaccess-to-remove-html-causing-a-500-error
Related
I have surely read and tested all the solutions found on Stackoverflow but couldn't make it work ... so here I am.
I'm trying to transform
example.com/?l=en
into
example.com/en/
( /en/ isn't supposed to exist on the server )
I use the following htaccess
DirectoryIndex index.php
ErrorDocument 404 https://example.com/
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
RewriteCond %{QUERY_STRING} (^|&)l=en($|&)
RewriteRule ^$ /en/?&%{QUERY_STRING}
RewriteCond %{QUERY_STRING} (^|&)l=ja($|&)
RewriteRule ^$ /ja/?&%{QUERY_STRING}
Since the last line triggers a 404 error, the errorDocument 404 kicks in.
The rule returns a "page not found" in the browser when I comment the errorDocument line.
Stuck again with htaccess. How can I add a 404 page to a specific subdirectory?
In my case the 404 page should only affect the "staging" subdirectory (and the directories inside of staging) but not the root or any other subdirectories placed in root.
http://example.com/staging
The code which is in my .htaccess file for the directory "staging":
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
Options -Indexes -MultiViews
RewriteEngine on
RewriteRule ^arbeiten/$ /staging/arbeiten/alle [R=301,L]
RewriteRule ^journal/$ /staging/journal/journal-alle [R=301,L]
ErrorDocument 404 /staging/404.html
Using the code above (and many others) results in a Internal Server Error.
Update #1 to my initial question.
In general, the code/answer provided to add a 404 not found page to a specific sub directory works:
ErrorDocument 404 /staging/404.html
The reason why it still does not work is because there must be some error in the overall .htaccess setup related to the code remove .html extension and/or redirect to a page in a folder.
Options -Indexes -MultiViews
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
RewriteRule ^arbeiten/$ /staging/arbeiten/alle [R=301,L]
RewriteRule ^journal/$ /staging/journal/journal-alle [R=301,L]
ErrorDocument 404 /at/404.html
I will continue to further investigate the setup in my .htaccess.
Update #2
Using the .htaccess script below allows to remove .html extensions, redirect pages to a specific page within a directory and basically a working 404 not found page within a specific sub directory on a Hostgator cPanel grid hosting (apache).
Options -Indexes -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]
RewriteRule ^arbeiten/$ /staging/arbeiten/alle [R=301,L]
RewriteRule ^journal/$ /staging/journal/journal-alle [R=301,L]
ErrorDocument 404 http://www.example.com/staging/404
Remaining 404 problem:
http://example.com/staging/blabla (404 works)
http://example.com/staging/folder/blala (404 works)
http://example.com/staging/folder/project-detail-pagexxx (404 works)
http://example.com/staging/folder/project-detail-pagexxx/ (404 does not work)
When there's 404 missing page error this URL is called:
https://domain.com/404/
Inside the 404 folder there is only a htaccess file that will redirect to language specific error pages (subfolders only with one index.html in them).
/
404
.htaccess
pt
index.html
en
index.html
htaccess file code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# If Accept-Language starts with 'pt',
# then redirect (only) subdirectory 'pt'
RewriteCond %{HTTP:Accept-Language} ^pt [NC]
RewriteRule ^/?$ %{REQUEST_URI}pt [L,NC]
# Otherwise, redirect to en
RewriteRule ^/?$ %{REQUEST_URI}en [L,NC]
</IfModule>
The final URL looks like this for Portuguese for example: domain.com/404/pt/
What do I need to change so that the URL will still redirect to the index.html in the language specific subfolder but will look like domain.com/404(with or without trailing slash)?
Thank you.
For silent rewrites to language specific 404 use this .htaccess in /404/ sub-directory:
ErrorDocument 404 default
DirectoryIndex index.html
RewriteEngine On
RewriteBase /404/
# If Accept-Language starts with 'pt',
# then redirect (only) subdirectory 'pt'
RewriteCond %{HTTP:Accept-Language} ^pt [NC]
RewriteRule ^/?$ pt/index.html [L]
# Otherwise, redirect to en
RewriteRule ^/?$ en/index.html [L]
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'm using the below in my htaccess file to strip out the .php extensions on my files. It works fine.
However, I also want to utilize ErrorDocument 404 /index.php to redirect any non existing urls back to the homepage. If I use ErrorDocument 404 /index.php alone in the htaccess, the redirects work fine. Also, if I use the below Rewrite code it successfully strips the php extensions. I would like both of these to work together, however.
For example, if a user enters "www.domain.com/register", they go to www.domain.com/register.php. But if they enter "www.domain.com/something", and something.php doesn't exist, then they redirect back to index.php. I've tried every permutation of both of these together in my htaccess to no avail. Any help would be appreciated.
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]
Not sure what the problem you're having is exactly, but if I change your rules to this, it works for me:
ErrorDocument 404 /index.php
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
So instead of checking if the request isn't accessing an actual file/directory that exists, check that the .php version of that file DOES exist.