I’m trying to redirect anyone surfing to http://forum.student.khleuven.be to http:// forum.kringraad.be. Both URL’s redirect to the same directory, basically. I tried the following using .htaccess:
Redirect permanent http://forum.student.khleuven.be/ http://forum.kringraad.be/
and also the following:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^forum\.student\.khleuven\.be$ [NC]
RewriteRule ^(.*)$ http://forum.kringraad.be/$1 [R=301,L]
</IfModule>
Both give me a 500 Internal Server Error. I double checked that mod_rewrite is enabled, and it is. I’m pretty clueless, to be honest Anyone know what to do?
asked Jun 30, 2010 at 18:32
David MDavid M
1971 gold badge2 silver badges6 bronze badges
3
Apparently the solution was this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^forum.student.khleuven.be$ [NC]
RewriteRule ^(.*)$ http://forum.kringraad.be/$1 [R=301,L]
</IfModule>
I just had to remove the escapes. I don’t know why I don’t have to escape those dots, but it works perfectly like this.
answered Jun 30, 2010 at 19:11
David MDavid M
1971 gold badge2 silver badges6 bronze badges
вот мой .htaccess
DirectoryIndex index.php
RewriteEngine On
# Optimize deflate
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
# Options +FollowSymLinks
# Accses in folder
Options All -Indexes
# Loading Errors
ErrorDocument 401 /error/401.php
ErrorDocument 403 /error/403.php
ErrorDocument 404 /error/404.php
ErrorDocument 500 /error/500.php
# Optimize deflate - немного ускоряем работу скрипта.
# Accses in folder - Запрещаем вывод каталога папок, как я видел у некоторых доступно по адресу http://mysiti.com/templates/Default/ - каталог как на ладони.
# Loading Errors - выводим собственно свою ошибку..
# Виджеты
RewriteRule ^dev/widget_comm(/?)+$ index.php?go=dev_wid&act=widget_comm [L]
# Отключаем вывод ошибок пользователям
php_flag display_errors off
#robots.txt
RewriteCond %{HTTP_USER_AGENT} !^yandex.* [NC,OR]
RewriteCond %{HTTP_USER_AGENT} !^googlebot.* [NC,OR]
RewriteCond %{HTTP_USER_AGENT} !^rambler.* [NC,OR]
RewriteRule ^robots\.txt$ / [L,R]
# Регистрация
RewriteRule ^reg/(/?)+$ index.php?go=register [L]
# Отзывы
RewriteRule ^reviews(/?)+$ index.php?go=reviews [L]
# Apps
RewriteRule ^apps(/?)+$ index.php?go=apps [L]
RewriteRule ^app([0-9]+)(/?)$ index.php?go=apps&act=app&id=$1 [L]
RewriteRule ^apps(/?)+$ /index.php?go=apps&act=search [L]
RewriteRule ^apps(/?)+$ /index.php?go=apps&act=view [L]
RewriteRule ^apps(/?)+$ /index.php?go=apps&act=mydel [L]
RewriteRule ^apps(/?)+$ /index.php?go=apps&act=install [L]
RewriteRule ^editapp/create(/?)+$ /index.php?go=editapp&act=create&id=$1 [L]
RewriteRule ^editapp/info_([0-9]+)(/?)+$ /index.php?go=editapp&act=info&id=$1 [L]
RewriteRule ^editapp/options_([0-9]+)(/?)+$ /index.php?go=editapp&act=options&id=$1 [L]
RewriteRule ^editapp/payments_([0-9]+)(/?)+$ /index.php?go=editapp&act=payments&id=$1 [L]
RewriteRule ^editapp/admins_([0-9]+)(/?)+$ /index.php?go=editapp&act=admins&id=$1 [L]
# Баги
RewriteCond %{QUERY_STRING} ^act=([a-z]+)
RewriteRule ^(.*)bugs $1index.php?go=bugs [QSA,L]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)
RewriteRule ^(.*)bugs $1index.php?go=bugs [QSA,L]
RewriteRule ^bugs(/?)+$ index.php?go=bugs [L]
# API
RewriteRule ^method/messages.get method/messages.get.php [L]
RewriteRule ^method/messages.set method/messages.set.php [L]
RewriteRule ^api(/?)+$ method/messages.set method/messages.set.php [L]
# Разработчикам
RewriteRule ^dev(/?)+$ index.php?go=developers [L]
RewriteRule ^dev/met(/?)+$ index.php?go=met [L]
RewriteRule ^dev/native(/?)+$ index.php?go=native [L]
RewriteRule ^dev/tt(/?)+$ index.php?go=tt [L]
RewriteRule ^dev/standalone(/?)+$ index.php?go=standalone [L]
RewriteRule ^dev/sites(/?)+$ index.php?go=sites [L]
RewriteRule ^dev/rules(/?)+$ index.php?go=rules [L]
# Статистика страницы пользователя
RewriteRule ^my_stats(/?)+$ index.php?go=my_stats [L]
# Страница юзера
RewriteRule ^u([0-9]+)(/?)+$ index.php?go=profile&id=$1 [L]
RewriteRule ^u([0-9]+)after(/?)+$ index.php?go=profile&id=$1&after=1 [L]
# Редактирование страницы
RewriteRule ^editmypage(/?)+$ index.php?go=editprofile [L]
RewriteRule ^editmypage/contact(/?)+$ index.php?go=editprofile&act=contact [L]
RewriteRule ^editmypage/interests(/?)+$ index.php?go=editprofile&act=interests [L]
RewriteRule ^editmypage/all(/?)+$ index.php?go=editprofile&act=all [L]
-
Вопрос задан
-
791 просмотр
Пригласить эксперта
Знаете как я поступаю в таких ситуациях? Когда не знаю где ошибка и нет подсказок
Делю код на несколько частей, поочередно удаляю один за другим и смотрю работает или нет, до тех пор пока не заработает, а потом уже работаю над последней удаленною частью, все очень просто, Ватсон
в /var/log/apache2/error.log смотрите, там будет указана, какая именно из директив htacces на этом сервере не работает.
Скорее всего нужно a2enmod rewrite или a2enmod filter
-
Показать ещё
Загружается…
21 сент. 2023, в 14:49
25000 руб./за проект
21 сент. 2023, в 14:33
5000 руб./за проект
21 сент. 2023, в 14:16
100000 руб./за проект
Минуточку внимания
I am trying to make 500, 404, etc. errors redirect. If you go to http://www.yoursite.com/934834984 it should redirect to http://www.yoursite.com/404. How do I make it do that? I tried using this code:
<meta http-equiv="refresh" content="0; url=http://yoursite.com/404" />
Keep in mind that I changed the domain to yoursite.com just for this question.
Raptor
53.3k45 gold badges231 silver badges366 bronze badges
asked Jan 25, 2014 at 4:05
7
In Apache you can use ErrorDocument
. So in your .htaccess
file in the root of your domain you can add this to the end of the file.
ErrorDocument 404 /404.html
The files have to exist. Here is some more info as well.
http://support.hostgator.com/articles/custom-error-pages
answered Jan 25, 2014 at 4:09
Panama JackPanama Jack
24.2k10 gold badges63 silver badges95 bronze badges
7
You need to edit the .htacess file (with notepad) in your domain’s root folder. Here are some common ones that you would add or change in the file. Obviously change the directory and locations (the part in italics). And don’t include the numbers. Or you can change them from your cpanel without having to mess with the .htacess file
- ErrorDocument 400 /errors/badrequest.html
- ErrorDocument 401 /errors/authreqd.html
- ErrorDocument 403 /errors/forbid.html
- ErrorDocument 404 /errors/notfound.html
- ErrorDocument 500 /errors/server.html
answered Jan 25, 2014 at 19:18
adigioiaadigioia
1,1889 silver badges10 bronze badges
ErrorDocument 500 /errors/server.html not work for me
show in browser HTTP ERROR 500
answered Jan 14, 2019 at 17:41
but when I encounter a 500 Internal Server Error, it gives the default Internal Server Error
The problem is that custom 500 error documents defined «late» in .htaccess
simply don’t get triggered for the majority of server errors — which is what’s likely happening here. As Aakash has already quoted, this may come under the realm of a «malformed request». If you check your error log it should state: «core:error».
You stand a better chance of the custom 500 error document being served if it is defined «early» in the main server config (or VirtualHost container).
In fact, it is a bit tricky to simulate a real error that will trigger the custom 500 error document defined in .htaccess
.
However, you can manually trigger a 500 error, which will call your custom error handler with something like the following:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^ - [R=500]
The check against the REDIRECT_STATUS
env var ensures that the internal request for the error document itself does not trigger another 500 error, but a direct request for the error document would also trigger a 500 error. This check is not necessary if you already have an exception in place for error documents.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
This does literally mean that in addition to the 500 error that resulted from the initial request, another 500 error was encountered when trying to serve the custom error document (that is called via an internal subrequest).
The custom error document itself is also processed by the Apache config/.htaccess
.
Generally, exceptions need to be made for custom error documents so they can be served without additional processing.
More info: I can get it to throw 500 if I type a trailing slash on a .html page that has had the «.html» removed…
Not sure that this is really part of your question, but this 500 error is the result of a rewrite loop. Specifically:
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
Request: http://example.com/test/
(with the extra slash)
In this case the %{REQUEST_FILENAME}
is /path/to/test
(no trailing slash), so the condition %{REQUEST_FILENAME}.html -f
is true (/path/to/test.html
does exist).
However, the URL-path that is captured by the RewriteRule
pattern does contain the trailing slash ie. test/
— it is not the same as the REQUEST_FILENAME
in this instance. So the URL gets incorrectly rewritten to test/.html
.
And the rewriting starts over again from the top (because the URL has changed)… test/.html.html
, test/.html.html.html
, etc. (Because the REQUEST_FILENAME
is always /path/to/test
.)
Yes, there is a rewrite loop for every URI coming from a subdomain here that doesn’t map to a filename or directory.
For example if the client comes in with mail.indst.eu/nofile
- Hit subdomain rule mail.indst.eu for condition !^/mail -> rewrite to mail.indst.eu/mail/nofile
- No longer matches !^/mail, doesn’t hit any external redirect rules, so falls through towards the end.
- Doesn’t match the rewrite exclusion for existing files or one of the hardcoded exclusions
- Hits the non-host-bound all-uri rule, gets rewritten to mail.indst.eu/index.php
- Now it matches again the subdomain rule mail.indst.eu for condition !^/mail -> rewrite to mail.indst.eu/mail/index.php
- No longer matches !^/mail, doesn’t hit any external redirect rules, so falls through towards the end.
- Hits the non-host-bound all-uri rule, apparently mail/index.php is not an existing file, so /mail/index.php gets rewritten to /index.php, now we are in an alternating repeat between /index.php and /mail/index.php
/nofile
/mail/nofile (does not exist)
/index.php
/mail/index.php (does not exist)
/index.php
/mail/index.php (does not exist)
...repeat last two...
If you place an index.php in the subdirectories used by the subdomains, then a non-existing file uri on a subdomain will end up at the matching subdomain/index.php files, which for some applications may be what you want.
If you want a serverlevel 404 response, then you have to exclude the subdomain hosts from the catchall rewrite to index.php.
For example
RewriteCond %{HTTP_HOST} !^mail\.indst\.eu$
RewriteCond %{HTTP_HOST} !^www\.statesanalytics\.com$
RewriteRule ^.*$ index.php [NC,L]
OR, which might be more succint, and allow more subdomains:
RewriteCond %{REQUEST_URI} !^/mail
RewriteCond %{REQUEST_URI} !^/sa
RewriteRule ^.*$ index.php [NC,L]
Then a non existing uri at mail.indst.eu/starting-path is server level non existing if it doesn’t exist in mail/starting-path
There are other techniques to prevent such loops. If you know that there would always at most be one internal redirect for example, and all rewriterules reside in this one .htaccess then you can do this as first rewriterule in your .htaccess:
RewriteCond %{ENV:REDIRECT_STATUS} !^$
RewriteRule ^(.*)$ - [L]
That stops further internal redirects from this .htaccess. But it would require you to put the external redirect rules that you have before the internal redirect rules. It would not stop the RedirectMatch rules.
Or, if you know that a catchall rewrite to index.php should always be the last word the end mod_rewrite processing with [END]. But in this case that would mean a 404 on the subdomain ends up with the main directory index.php which may not be what you want.
RewriteRule ^.*$ index.php [END]