Site enters an endless loop when i try to move error pages in subfolder
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([-\w.]+)/?$ profile.php?u=$1
DirectoryIndex home.php
ErrorDocument 400 /400.php
ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php
ErrorDocument 502 /502.php
ErrorDocument 504 /504.php
and after i move my pages
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([-\w.]+)/?$ profile.php?u=$1
DirectoryIndex home.php
ErrorDocument 400 /errors/400.php
ErrorDocument 401 /errors/401.php
ErrorDocument 403 /errors/403.php
ErrorDocument 404 /errors/404.php
ErrorDocument 500 /errors/500.php
ErrorDocument 502 /errors/502.php
ErrorDocument 504 /errors/504.php
(also tried with out the slash befor error)
otherwise works perfect . Can the problem be that i'm running on localhost(xampp) or the virtual host?
Related
I am trying to add an ErrorDocument (page not found) to my website.
I have the errorpage.php
<?php
http_response_code(404);
?>
...
<h1>page not found</h1>
And my .htaccess looks like this:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /$1.html [QSA,L]
Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
ErrorDocument 404 http://www.example.com/errorpage.php
ErrorDocument 500 http://www.example.com/errorpage.php
If I open a not existing page like: "www.example.com/xxx" I get redirected to: "www.example.com/errorpage.php" but with the status code:
302 found
404 not found
and this is bad I only need the status code "404 not found".
Why do I get two status codes and the redirect from found to not found?
Maybe your request is non www and first redirect 302 to www and then get http404 status from errorpage.php, remove host and check. You can also move this lines to top of your .htaccess file. Maybe it help
ErrorDocument 404 /errorpage.php
ErrorDocument 500 /errorpage.php
I had to add another RewriteCond:
RewriteCond %{REQUEST_URI} .!errorpage\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /$1.html [QSA,L]
ErrorDocument 404 /errorpage.php
ErrorDocument 500 /errorpage.php
I have the following codes in my htaccess file which removes .php extension from page name and redirect to a 404 page when a trailing slash is inserted at the end of a page name in the following directories.
example.com/file [redirecting]
example.com/file/ [not redirecting]
example.com/folder/file [redirecting]
example.com/folder/file/ [not redirecting]
example.com/folder/folder/file [not redirecting]
example.com/folder/folder/file/ [not redirecting]
I have tried several htaccess codes but 5. doesn't redirects. I will like 5. to be redirecting like 1. and 3.
CODE
# error pages
ErrorDocument 203 /error/203.php
ErrorDocument 204 /error/204.php
ErrorDocument 301 /error/301.php
ErrorDocument 400 /error/400.php
ErrorDocument 401 /error/401.php
ErrorDocument 403 /error/403.php
ErrorDocument 404 /error/404.php
ErrorDocument 405 /error/405.php
ErrorDocument 406 /error/406.php
ErrorDocument 500 /error/500.php
ErrorDocument 502 /error/503.php
ErrorDocument 503 /error/503.php
ErrorDocument 504 /error/504.php
Options -Indexes
RewriteEngine on
# remove .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^.]+)$ $1.php [NC,L]
# rewrite index page
RewriteRule ^login index.php [NC,L]
# rewrite profile page
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
# site.com/profile.php
RewriteRule ^([a-zA-Z0-9_.]+)$ profile.php?tg_u=$1 [NC,L]
#RewriteRule ^([^.]+)$ $1.php [NC,L]
# block access to htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
I've recently been experiencing problems with my website which appear to be related to my .htaccess file.
The main issues are that some pages are redirecting to themselves, while others show a 300 Multiple Choices error page, which only ever has 1 page in the list.
The original setup of my .htaccess file was to both connect up my custom error pages, but also to remove the extension displayed at the end of each page (.e.g. /about.php would appear as /about).
Below is the contents of the current .htaccess file, along with a link to my site.
Options -Indexes
#RewriteEngine On
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteBase /
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Added automatically by Gridhost panel Tuesday 29th of April 2014 07:12:58 PM
ErrorDocument 404 /404.php
ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 405 /405.php
ErrorDocument 500 /500.php
DirectoryIndex welcome.html index.html index.htm index.php
www.andrewcourtney.co.uk
I have contacted my website host regarding this, though they were not able to help and advised I post on a forum to find out what is wrong.
I've also adapted the htaccess file to the following but to no avail (checked within Incognito Mode):
Options -Indexes
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
ErrorDocument 404 /404.php
ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 405 /405.php
ErrorDocument 500 /500.php
DirectoryIndex welcome.html index.html index.htm index.php
Please could someone advise?
Cheers,
Andrew
EDIT:
I've now updated my htaccess to include the lines for MultiViews and CheckSpelling Off but now all pages just redirect back to the homepage?
Options -Indexes
Options -MultiViews
CheckSpelling off
#RewriteEngine On
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteBase /
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Added automatically by Gridhost panel Tuesday 29th of April 2014 07:12:58 PM
ErrorDocument 404 /404.php
ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 405 /405.php
ErrorDocument 500 /500.php
DirectoryIndex welcome.html index.html index.htm index.php
I am trying to force the www version of my site through the .htaccess file, but it throws me an ERR_TOO_MANY_REDIRECTS error. Can you help me?
.htaccess:
RewriteEngine On
RewriteBase /
#Redirect to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#Access .php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
#Errors
ErrorDocument 403 /error.php
ErrorDocument 404 /404.php
ErrorDocument 405 /error.php
ErrorDocument 408 /error.php
ErrorDocument 500 /error.php
ErrorDocument 502 /error.php
ErrorDocument 504 /error.php
Thank you.
I found the problem.
I hosted my website on OpenShift and added the domain with www as an alias and the domain without www as another alias.
The one with www was set as default. I set the one without www as default and it worked.
EDIT: It worked for a day and then it stopped working ...
Finally, I gave up and used this.
if(document.domain == "domain.com")
{
window.location.replace("http://www.domain.com" + document.location.pathname + document.location.search);
}
I have recently setup a index.php redirect through .htaccess. The idea here is to negate duplicate content issue that crops up when a site has both an index.php and / (homapage) getting indexed.
Here is my original .htaccess
Options -Indexes
ErrorDocument 403 /customerrors/403.html
ErrorDocument 401 /customerrors/401.html
ErrorDocument 400 /customerrors/400.html
ErrorDocument 500 /customerrors/500.html
ErrorDocument 404 /customerrors/404.html
Pretty basic.
I used the technique listed here to redirect the index.php to /.
http://www.askapache.com/htaccess/redirect-index-blog-root.html
It works great too. One issue is, it breaks the 404 pages.
This is the modified .htaccess that is breaking the 404 pages.
RewriteEngine On
RewriteBase /
DirectoryIndex index.php
RewriteCond %{http_host} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://example.com/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Options -Indexes
ErrorDocument 403 /customerrors/403.html
ErrorDocument 401 /customerrors/401.html
ErrorDocument 400 /customerrors/400.html
ErrorDocument 500 /customerrors/500.html
ErrorDocument 404 /customerrors/404.html
So if a user types in or goes to www.example.com/dafjkadbfda instead of serving 404 page, the URL remains the same(in this case the broken one) and severs the index.php page.
This in turn is opening another can of worms. All those broken pages are coming up as duplicate content and meta.
Is there another way to write the .htacess redirect that will take into account the 404 pages? Seems like that is the conflict here.
Thanks in advance.
This part of the .htaccess is the one that "breaks" the 404:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
You could manage the problem by sending a 404 error from your PHP script:
if ($not_a_valid_page){
header("$_SERVER[SERVER_PROTOCOL] 404 Not Found");
readfile("./customerrors/404.html");
die();
}