htaccess two commands in the same file - php

Hi i have my htaccess with this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.htm [L]
ErrorDocument 404 /404.htm
if i erase the first 4 lines, the other piece of code (which redirects de 404 page when there's a page not found) does not work anymore.
Any ideas?
I also want that when it redirects to 404.htm, it does not show on the url box, this url
http://www.mypage.com/404.htm
i want to show this
http://www.mypage.com/fail/search-by-the-user
for example.

Try following .htaccess
RewriteBase /
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ /$1.html
ErrorDocument 404 /404.html
NOTE: if you are working at localhost then please mention proper RewriteBase

Related

.htaccess rewrite ErrorDocument not being redirected

I removed the php directory from my URL and remove the .php extension.
But now I am trying to add my 404/500 error to make my website look nice.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /php/$1 [QSA,L]
ErrorDocument 404 /errors/404.php
ErrorDocument 500 /errors/500.php
When I redirect the errorDocument to an external page it works but not to my local file. I get a 500 Internal Server error. I could make a workaround and just make it go to an external page what will be my page. But that's not really clean.
Is anyone able to help me with this?
I think you must match against DOCUMENT_ROOT as well as add this RewriteCond %{REQUEST_FILENAME}\.php -f before adding .php like this :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/php%{REQUEST_URI}\.php -f
# or you could do it like this
# RewriteCond %{DOCUMENT_ROOT}/php%{REQUEST_URI} -f
# without php according to your setting
RewriteRule ^(.*)$ /php/$1 [QSA,L]
ErrorDocument 404 /errors/404.php
ErrorDocument 500 /errors/500.php
because whenever you send wrong request to any target destination you must make sure that target will handle it and there are enough conditions to classify which one should go , otherwise server will generate error.

htaccess 404 redirect does not working

I have a .htaccess file to manage rewrite_rules on my website. I also set a 404 redirect command to navigate visitors to a specefied page if they enter a wrong URL. But this redirect does not working and I get 500 Internal Server Error if visitor request an invalid URL. Below is some part of my codes:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([0-9]+)/([^/]+)/?$ index.php?lang=$1&page=$2&id=$3&des=$4 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/(p[0-9]+)/?$ index.php?lang=$1&page=$2&pn=$3 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?lang=$1&page=$2 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?lang=$1&page=home [NC,QSA,L]
ErrorDocument 404 http://www.mywebsite.com/404/
What is missing in my code that causes this problem?
The culprit, by seeing all of your .htaccess rules is:
RewriteRule ^(.*)$ $1 [NS,E=no-gzip:1,E=dont-vary:1]
Giving your sample URL http://www.domain.com/en/news/1/something/test/, when a URL does not match with defined rules it hits this last rewrite rule and it falls into an infinite loop:
applying pattern '^([^/]+)/([^/]+)/([0-9]+)/([^/]+)/?$' to uri 'en/news/1/something/test/'
...
applying pattern '^([^/]+)/([^/]+)/(p[0-9]+)/?$' to uri 'en/news/1/something/test/'
...
applying pattern '^([^/]+)/([^/]+)/?$' to uri 'en/news/1/something/test/'
...
applying pattern '^([^/]+)/?$' to uri 'en/news/1/something/test/'
...
applying pattern '^(.*)$' to uri 'en/news/1/something/test/'
...
Then we have a recursion start:
applying pattern '^([^/]+)/([^/]+)/([0-9]+)/([^/]+)/?$' to uri 'en/news/1/something/test/'
...
Although a URL like http://www.domain.com/en/news/1/wrong is not found, it's caught by a defined rule (first rule) and we don't encounter any recursion. So this internal error is not thrown on all not found pages but those that are not caught by a rule.
I don't see it right, just remove it.
you should put
<IfModule mod_rewrite.c>
RewriteEngine On
in the start and
</IfModule>
in the end of your .htaccess code
Try adding this rule to the top of your htaccess:
RewriteEngine On
RewriteRule ^404/?$ /pages/errors/404.php [L]
Then under that :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ http://domain.com/404/ [L,R]
You can use an EroorDocument directive to forword non existent requests to a specific destination
Test this in an empty .htaccess file
ErrorDocument 404 /page.php
Check below code, I have write with various scenario.
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]
It seems your mentioned URL after ErrorDocument is not valid or is not reachable.
So, what happening is that when it gets a 404 it redirects to your URL, and it gets a 404 again.
Forms a recursive loop.
Getting my point. ?
Try with a simple static html page.
I hope this shall work.
Custom 404 will never work if you add domain like http://www.mywebsite.com/404/ (it's not coding karma), when you type url like this it will ended up in 500 error. It means it executed 404 but redirected user to 500 error due to wrongly configured htaccess.
When error comes in series of 4XX are knows as client side errors and 5XX known as server side errors. For detailed info, 2XX known as Success & 3XX known as Redirection.
So here is quick try (i assume you have 404 html file)
ErrorDocument 404 /404.html
instead of
ErrorDocument 404 http://www.mywebsite.com/404/
I also have tested this code at my side and it worked. Please try out
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([0-9]+)/([^/]+)/?$ index.php?lang=$1&page=$2&id=$3&des=$4 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/(p[0-9]+)/?$ index.php?lang=$1&page=$2&pn=$3 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?lang=$1&page=$2 [NC,QSA,L]
ErrorDocument 404 /404.html
Try this one:
ErrorDocument 404 /404/
Try adding before rewriterules
RewriteEngine On

An "internal server error" aka 500 has perplexed me entirely - .htaccess slash php madness

I know what this error is, but I'm sure it has something to do with my recent change of .htaccess or something missing from it. I added a few lines of rewrite code to change "something.php" to "something" - it worked fine. When testing my website online for the last few stages of development, I wanted to see what type of errors I'll get by attempting to access folders, and files - 404, 403, 401 etc.
Lets get to the point! When I entered ....com/press or ....com/press.php it will take me to the page as expected. When I enter ...com/press/ I get a 500 error - yes it has a slash, but shouldn't I be getting a 404 error? I say this because the file press.php exists, but not the folder ...com/press/ - it doesn't even show my own custom error page!
After taking a look at what I put in this .htaccess file, I also want some additional help with .htaccess. Index should be index alone, the server shouldn't accept .php even if it is, if you know what I mean!
Thanks in advance
Code in .htaccess:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]
Modify your rule as:
ErrorDocument 404 default
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Index should be index alone, the server shouldn't accept .php even if
it is
You can use this so that if they using the php extension, it will redirect.
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /(.+)\.php
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)/?$ /$1.php [L,QSA]

Clean Url Error 404

Just wanted to customize Error 404 in my htaccess. The problem is when the user goes to Error 404 page. The URL is not clean compares to others. URL looks like these index.php?page=page-not-found .. How can I make it http://example.com/page-not-found.htm or http://example.com/page-not-found/ ??? By the way, have a look on my codes.
Options All -Indexes
RewriteEngine On
ErrorDocument 404 http://example.com/index.php?page=page-not-found
RewriteRule ^([^/]*)\.htm$ /website/index.php?page=$1 [L,QSA]
RewriteRule ^([^/]*)/lesson/([^/]*)\.htm$ /thesis/index.php?page=$1&lesson=$2
Any idea guys? By the way, thanks..
Remove http:// from your 404 directive to avoid server making a full redirect for 404:
Options All -Indexes
RewriteEngine On
ErrorDocument 404 /index.php?page=page-not-found
RewriteRule ^([^/.]+)\.htm$ /website/index.php?page=$1 [L,QSA]
RewriteRule ^([^/]+)/lesson/([^/.]+)\.htm$ /thesis/index.php?page=$1&lesson=$2 [L,QSA]
Please try this one.First remove your extensions from all files and try this one
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^p/([0-9]+)$ /p.php?photo=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(Joe|Mao|Napoleon|Gandi)$ /profile.php?username=$1 [QSA,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)$ /$1.php [QSA,L]

Mod_rewrite not redirecting properly

first of all I know there are questions about my problem I tried few of them, but unsuccessfully.
I am trying to get:
http://example.com/project/text
from
http://example.com/projects.php?c=text
I have this .htaccess:
RewriteEngine on
RewriteRule ^project/(.*)$ projects.php?c=$1 [L,QSA]
ErrorDocument 404 404.php
When I open the site i.e. http://example.com/project/123456 I get nothing(Blank site).
Thank you for you help.
You can try:
ErrorDocument 404 404.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^project/(.+)$ projects.php?c=$1 [L,QSA,NC]

Categories