Htaccess not rewritting file in sub directories - .htaccess

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>

Related

ErrorDocument in htaccess

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

"300 Multiple Choices" error due to htaccess file

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

Htacces entering loop after moving error documents

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?

Redirects issue and htaccess

I am having a redirect issue. I have my htaccess setup to do the following...
1. Remove PHP extensions from URL
2. Add custom error pages
3. Force some 301 redirects
But there is a huge problem with the redirects...
If a user goes to ...
www.mysite.com/articles/jewelry/types.php
... then, based on the 2nd 301 redirect, they should be sent to ...
www.mysite.com/article/medieval-jewelry-types-and-functions/
... however, they are redirected to ...
www.mysite.com/article/medieval-jewelry/types
If they go to ...
www.mysite.com/articles/picks/A_1.php
... they should be redirected to...
www.mysite.com/article/picks-tools/
... but they get sent to ...
www.mysite.com/article/how-to-pick/
It looks like the redirect is changing PART of the url ... probably because of the .php extension (but that is just speculation - because I know very little about editing the htaccess file). Any ideas?
Below is most of my htaccess file, with some comments on what MIGHT be causing the issue. Any idea how to fix it?
# enable the rewrite engine
RewriteEngine On
# Set your root directory
RewriteBase /
# remove the .php extension
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index and reference the directory
RewriteRule (.*)/index$ $1/ [R=301]
# remove trailing slash if not a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# forward request to html file, **but don't redirect (bot friendly)**
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
ErrorDocument 400 /400.php
ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
ErrorDocument 405 /405.php
ErrorDocument 408 /408.php
ErrorDocument 414 /414.php
ErrorDocument 500 /500.php
ErrorDocument 502 /502.php
ErrorDocument 504 /504.php
# example of redirects
Redirect 301 /articles/index.php /articles/
# it looks like the redirect below is conflicting with...
Redirect 301 /articles/jewelry/ /article/medieval-jewelry/
# ... this redirect.
Redirect 301 /articles/jewelry/types.php /article/medieval-jewelry-types-and-functions/
# And this redirect...
Redirect 301 /articles/picks/ /article/how-to-pick/
Redirect 301 /articles/picks/index.php /article/how-to-pick/
# ... is conflicting with this one...
Redirect 301 /articles/picks/A_1.php /article/picks-tools/
Problem seems to be wrong order of rules, incorrect regex and mixing of mod_rewrite rules with Redirect (mod_alias) rules. Try this:
ErrorDocument 400 /400.php
ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
ErrorDocument 405 /405.php
ErrorDocument 408 /408.php
ErrorDocument 414 /414.php
ErrorDocument 500 /500.php
ErrorDocument 502 /502.php
ErrorDocument 504 /504.php
# enable the rewrite engine
RewriteEngine On
# Set your root directory
RewriteBase /
# example of redirects
RewriteRule ^articles/index\.php$ /articles/ [L,NC,R=302]
# it looks like the redirect below is conflicting with...
RewriteRule ^articles/jewelry/?$ /article/medieval-jewelry/ [L,NC,R=302]
RewriteRule ^articles/jewelry/types\.php$ /article/medieval-jewelry-types-and-functions/ [L,NC,R=302]
RewriteRule ^articles/picks/?$ /article/how-to-pick/ [L,NC,R=302]
RewriteRule ^articles/picks/index\.php$ /article/how-to-pick/ [L,NC,R=302]
RewriteRule ^articles/picks/A_1\.php$ /article/picks-tools/ [L,NC,R=302]
# remove the .php extension
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index and reference the directory
RewriteRule (.*)/index$ $1/ [R=301]
# remove trailing slash if not a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# forward request to php file, **but don't redirect (bot friendly)**
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

.htaccess redirect of index.php is breaking 404 pages

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();
}

Resources