htaccess 404 Stopped Working - apache

I had a custom 404 error redirect page working just fine through htaccess. However, after adding new code to htaccess, it stopped working. What's causing the conflict and how can I fix it?
EDIT: I've tried putting the ErrorDocument line at the top of the page and it still doesn't work.
htaccess code:
RewriteEngine On
#Removes www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule . http://%1%{REQUEST_URI} [R=301,L]
#Redirects to extensionless php urls
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^&\ ]+).php
RewriteRule .* /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.php [L]
#Redirect nonexistent files to a 404 page (not working)
ErrorDocument 404 /404.html

The problem is not about putting ErrorDocument in the top or in the bottom of your htaccess.
You have an infinite loop because of your rule (the one rewriting to php extension).
You need to check if it exists before rewriting it, otherwise you'll get a loop conflict between your rule and ErrorDocument.
You can replace your current code by this one
RewriteEngine On
#Remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
#Hide php extension (only for existing files)
RewriteCond %{THE_REQUEST} \s/(.+)\.php(?:\s|\?) [NC]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ /%1? [R=301,L]
#Redirect to extensionless php urls (only if it exists)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L]
#Redirect nonexistent files to a 404 page
ErrorDocument 404 /404.html

put the errorDocument statement at the top of the file. Above the RewriteEngine Statement.
This is the solution, however I can't tell you why the above htaccess code causes that to fail.

Related

How to manipulate the URL with .htaccess for certain links

How can I manipulate the url by using .htaccess to make the url below work and shorten the size?
I successfully removed the .php, .html extension from the url, but don't have a clue on how to manipulate the character positions and remove the query variable name.
Example:
www.website.com/blog?language=en-us
to:
www.website.com/en-us/blog
RewriteEngine On
DirectoryIndex index.php
ErrorDocument 404 /public_html/404.php
ErrorDocument 500 /public_html/500.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
With your shown samples, please try following htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect 301.
RewriteCond %{THE_REQUEST} \s/(blog)\?language=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Internal rewrite to index.php file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)/?$ index.php?param1=$1&language=$2 [QSA,NC,L]

Is there any function to change the URL Path of website?

I would like to change the path of the website from http://localhost/en-in/pan
to http://localhost/en-in/individual/pan
without creating a folder individual.
Kindly let me know if there is anything.
Following is the .htaccess file
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
RewriteRule ^(.*)index$ /$1 [R=301,L]
RewriteEngine On example.com
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
ErrorDocument 404 https://www.example.com/404
ErrorDocument 500 https://www.example.com/404
# or map them to one error document:
# ErrorDocument 404 /pages/errors/error_redirect.php
RewriteEngine On
RewriteBase /en-us
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /pages/errors/404.php [L]
# or map them to one error document:
#RewriteCond %{REQUEST_URI} ^/404/$
It depends on the server you are running on.
On apache, this can be achieved by creating a .htaccess file on your root directory of the website and add there the Redirect command:
Redirect /en-in/pan /en-in/individual/pan
Create a .htaccess file under your base directory (public_html or www) and put the content:
RewriteEngine On #remove this line if it doesn't work
RewriteRule en-in/individual/pan(/?)$ en-in/pan [QSA,L]
Then you can redirect your traffic from active link to the new link

Deleting a directory from the path to the URL using .htaccess

There is a link like
domain.com/dir/page
It displays the contents of the page page.php, which is located in the dir directory of the site.
Current view .htaccess at the root of the site:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule .* http://%1/$0 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php/
RewriteCond %{HTTPS} =off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [QSA,L]
ErrorDocument 404 /404.php
How do I replace the URL with domain.com/dir/page to domain.com/page (that is, remove dir from the path)?
P.S. It is understood that the solution should work for all URLs of the specified type.
Try using this in your .htaccess file, make sure it is above any other rules that you have:
RewriteEngine On
RewriteRule ^dir/(.*)$ /$1 [L,R=301]
Clear your cache before you test this.

htaccess point request to sub-directory if file doesn't exists and does under the subdirectory

I'm having a really hard time with trying to get this to work so I have a sub-directory example.com/projects with a directory under that called test and I'd like it if someone typed example.com/test it would show the file from example.com/projects/test but if the file does not exist I would like it to post a 404 error.
Additionally if possible I would like any time example.com/projects/test was typed it would redirect to example.com/test instead but if someone goes to just projects/ I want them to be able to stay there.
I have tried the following .htaccess files so far
This works almost perfectly passing through the files unless the file doesn't exist in which it throws a 500 Internal Server Error
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ projects/$1 [PT]
I thought something like this might work to only pass through if the file existed under projects but doesn't work at all:
RewriteCond projects/%{REQUEST_FILENAME} -f
RewriteCond projects/%{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ projects/$1 [PT]
For the second part pointing anything that goes to /projects/test to strip /projects I tried this (placed before the code above)
RedirectMatch ^/projects/(.+)$ /$1
Only to find a redirect loop. How can i prevent this?
My current .htaccess file for reference:
ErrorDocument 404 /404.html
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example2\.com$
RewriteRule ^/?$ "http\:\/\/example3\.org" [R=301,L]
#RedirectMatch ^/projects/(.+)$ /$1
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ projects/$1 [PT]
You can use these rules in your root .htaccess:
DirectoryIndex index.html
RewriteEngine On
RewriteCond %{THE_REQUEST} /projects/(\S+)\sH [NC]
RewriteRule ^ /%1 [L,R=302,NE]
RewriteCond %{DOCUMENT_ROOT}/projects/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/projects/$1 -d
RewriteRule ^(.+)$ projects/$1 [L]

File download links returning 404 after .htaccess rewrite

I rewrote my .htaccess to define a couple of redirects:
non-www to www
feed to blogs/feed
admin to blogs/wp-admin
All these redirects are working perfect. However, this seems to have broken my file download links. For example, I have a download links pointing to:
Mysitedomain/decks/50_Essential_Spanish_Verbs.apkg
I know for a fact that the file IS there. Yet every time I click the link, it throws a 404 instead of letting you download the file. Is my htaccess rewrite to blame for this issue? Any suggestions on how to fix it?
Here's what my .htaccess currently looks like:
<IfModule mod_rewrite.c>
RewriteEngine on
Header set Access-Control-Allow-Origin "*"
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([a-z.]+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
#RewriteRule ^/(.*)$ http://www.thesite.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
#Rewrite rule for blog feed
RewriteRule feed blogs/feed/burrorss [NC,L]
#Rewrite rule for blog admin
RewriteRule admin blogs/wp-admin [NC,L]
RewriteRule ^(.*) /index.html [NC,L]
</IfModule>