-4

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?

revo
  • 47,783
  • 14
  • 74
  • 117
Mohammad Saberi
  • 12,864
  • 27
  • 75
  • 127
  • Please add on first line. RewriteEngine on – Dharmendra Jul 21 '16 at 06:38
  • @Alex this is just a part of my .htaccess file. It contains what you said – Mohammad Saberi Jul 21 '16 at 06:52
  • This part of your .htaccess file has no problem or I can't reproduce it. But for **500** errors you may take a look at logs and I doubt if you have done this. Whether it is caused by Apache or PHP, `error.log` and `php_errors.log` files are considered to be checked. If URL to your error page is supposed to display output from some PHP codes then probably that's it. – revo Jul 23 '16 at 11:20
  • No error in your `http://www.mywebsite.com/404/` page ? You use absolute links in this page? – Croises Jul 23 '16 at 12:25
  • Can you add a `LogLevel alert rewrite:trace6` in your vhost config and see what kind of output shows up in your error log? (assuming apache 2.4, of course - see http://stackoverflow.com/a/9632952/866762) – Will Jul 24 '16 at 04:29
  • @Croises no error. It works and show custom 404 page – Mohammad Saberi Jul 24 '16 at 09:16
  • 1
    @MohammadSaberi, whenever apache falls with **500 Internal Server Error** it always write some errors in its **error.log**. Did you checked that file right after you get this error? And I think you should post you entire `.htaccess` so we can reproduce problem and find solution. – spirit Jul 25 '16 at 22:39
  • @spirit this is the code I can see in the logs: `Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.` – Mohammad Saberi Jul 26 '16 at 04:08
  • @MohammadSaberi, there you go. Next step: add to your `` config a directive `LogLevel debug` repeat error and update your question with log-records regarding that error. – spirit Jul 26 '16 at 10:04
  • 1
    One more thing. Can you show your full `.htaccess` and an example of **"wrong url"**? – spirit Jul 26 '16 at 10:07
  • @spirit, a sample wrong URL is like this: `http://www.domain.com/en/news/1/something/test/` – Mohammad Saberi Jul 28 '16 at 14:31
  • @MohammadSaberi, again, show your full `.htaccess`. With this part everything works fine, the problem isn't reproduced. I think the problem somewhere else in your `.htaccess` – spirit Jul 28 '16 at 19:56
  • @MohammadSaberi, if you have any `.htaccess` in your `/404/` folder, please show it as well. – spirit Jul 28 '16 at 20:15
  • @spirit `404` is not a folder. – Mohammad Saberi Jul 30 '16 at 10:13
  • Those who downvoted this question, please tell me why you made that decision? – Mohammad Saberi Jul 30 '16 at 10:15
  • have you check the edits? – Sachin G. Jul 30 '16 at 11:47
  • 1
    @MohammadSaberi, don't you hearing me? show me your **full .htaccess**. I'm pretty sure there is an error in it. Especially after your words: "`/404/` is not a folder". – spirit Jul 30 '16 at 20:58
  • One of those downvotes is mine since you do not care about commentators. – revo Jul 30 '16 at 21:03
  • @revo I answered most of comments. Some of them were similar and repetitive. Anyway, .... – Mohammad Saberi Jul 31 '16 at 04:05
  • @spirit: https://jsfiddle.net/dozwsz1L/ – Mohammad Saberi Jul 31 '16 at 04:10
  • @MohammadSaberi, that's not true =). I've asked for your full `.htaccess` several times during several days =) – spirit Jul 31 '16 at 17:21
  • You are right. Due to what you said I put it finally. I told it was my mistake to put that with delay. I came back here after finding problem and absolutely your answer would be my choice if you found the problem too. But you had not any answer at that time (you had only comments and . However I thank you for attentions and guidances. – Mohammad Saberi Jul 31 '16 at 18:28

9 Answers9

1

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.

revo
  • 47,783
  • 14
  • 74
  • 117
  • I found your answer before coming to this page again, and did you found it too. Thank you, and also thanks Spirit for your attentions and helps – Mohammad Saberi Jul 31 '16 at 09:30
  • You are welcome. You would receive an early answer if you take more care about comments and or providing information. @MohammadSaberi – revo Jul 31 '16 at 09:45
  • @MohammadSaberi, congrats! =) – spirit Jul 31 '16 at 19:48
0

you should put

<IfModule mod_rewrite.c>
    RewriteEngine On

in the start and

</IfModule>

in the end of your .htaccess code

0

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]
Muhammad Awais
  • 463
  • 1
  • 6
  • 17
0

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
Amit Verma
  • 40,709
  • 21
  • 93
  • 115
0

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]
0

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.

cedzz
  • 389
  • 2
  • 9
0

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
Sachin G.
  • 528
  • 4
  • 9
-1

Try this one:

ErrorDocument 404 /404/
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
-2

Try adding before rewriterules

RewriteEngine On