Solved

400 Bad Request

Posted on 2006-11-04
15
439 Views
Last Modified: 2012-06-27
Hello everybody:

On my site, I recieve a 400 Bad Request error when I try to access a directory without putting a slash after it (but it works if I do put the slash after it). Could you please tell me how to fix this?

URGENT, 500 Points
0
Comment
Question by:jsev1995
  • 7
  • 2
  • 2
  • +3
15 Comments
 

Expert Comment

by:NNDth
Comment Utility
Hello,

This error, 400 bad request, means that a request for a URL has been made but the server is not configured or capable of responding to it. So, they only thing you can do is to contact your hosting company and ask them to configure the server in order to make it work.

I' d reccoment to make a custom 400 Error page, in order to inform your visitors that they have to type the splash at the end of the URL.

You can make a cusom 400 Error page by doing this:

1)Open notepad and make a file that contain this:

ErrorDocument 400 /dir_of_the_custom_error_page

2)Replace the "/dir_of_the_custom_error_page" with the directory of your custom 400error page. e.g. /400error.html - (You have to create it first)
3) Save the file as .htaccess at your index directory [this is the same directory with your main page, the index.html(/.php/.htm/etc)]. NOTE: you have to save the file as .htaccess, DO NOT CHANGE IT! DO NOT TOUCH IT!
4)Upload it to your main directory and you are done!

I hope my answer is helpful.
0
 
LVL 3

Author Comment

by:jsev1995
Comment Utility
I am aware of how to use ErrorDocument. That does not answer my question, I don't want to tell people they have to add a slash, as ease of use is #1 in my webapp. Could you write me a RewriteRule or something similar to redirect it to the address, but with a slash? I know a lot about .htaccess, but I don't know how to correctly write RewriteRules to redirect things.
0
 

Expert Comment

by:LeonardCooper
Comment Utility
Hello,

No client should perform any intelligence in determining what to do with a URL, whether to add a slash, add a file name etc (Yes yes, we all know IE tries to get clever and does things like domain names etc etc)

This is a function of the webserver.
You do not mention what webserver you are using?
(http://mysite.com/stuff)
Generally, without the slash, the webserver looks for a file by this name "stuff". If it does not find one, it should "add a slash" and look for a directory by this name, and the load the configure DEFAULT FILE. (index.html, index.htm, index.php, default.htm, default.asp etc.)

So, some things to check is the confgured default page to load. Ensure that you have this default page in the directory in question. If not, consider changing the server config to suit your requirements - but remember that this will now be the rule!

That said, some times things go arwy on apache for various reason. In this case you can make use of MOD_REWRITE. (apache 2.x generally behaves itself in my experience, but this is useable in 1.3 and 2.x)

Apache documentation:
http://httpd.apache.org/docs/1.3/misc/rewriteguide.html
specifically the section titled: Trailing Slash Problem

RewriteEngine  on
RewriteBase    /~quux/
RewriteRule    ^foo$  foo/  [R]

This is a powerful module and can do many interesting things, for example, we use it so that ANY file requesting in a drectory is "re-written" to call a single file - so if you called somefile.php it will actually load defaultfile.php
    RewriteEngine on
    RewriteRule ^/dl/(.*)$ /dl/getcont.php

On apache, this can be included in a .htaccess file (if allowed!).

But I digress. Let us know what server and version you are using and the default file. Good starting place to look.

Leonard
0
 
LVL 3

Author Comment

by:jsev1995
Comment Utility
Oh sorry, I'm on apache. Hostgator hatchling, to be exact. I tried this but it didn't work:

RewriteEngine  on
RewriteBase    /jobtracker
RewriteRule    ^jobtracker$  /jobtracker/  [R]

Could you please fix it for me? Thanks.
0
 
LVL 23

Expert Comment

by:rama_krishna580
Comment Utility
Hi,

look at here...
http://plone.org/documentation/tutorial/plone-apache/virtualhost

1.If http://example.com/foo/bar does not exist, redirect to http://other.example.com/foo/bar. (Put this in an .htaccess file in your top-level web directory.)
# .htaccess in root of example.com
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://other.example.com/$1 [R]

2.Handle all requests for top-level .html files and files with no extensions (http://example.com/foo, http://example.com/foo.html) with a single PHP program /foo/show.php. Also, ignore trailing characters in set { : ; , . } so URLs like "http://example.com/foo." can be copied-n-pasted from plain text sentences by inattentive users.
# .htaccess in root of example.com
RewriteRule ^/?([^/]*\.html?|[^\./]*)[:;,\.]*$ /foo/show.php [L,NS]Examples:
http://tomclegg.net/rewriterule
http://tomclegg.net/rewriterule.html;

3.Redirect GET requests for http://example.com/foo/bar to http://example.com/bar (and /foo/bar.html to /bar.html). Handle POST requests with PHP program rather than attempting to redirect a POST (which is unlikely to work well).
# .htaccess in foo folder in example.com's document root
RewriteEngine On
RewriteCond %{REQUEST_METHOD} GET
RewriteRule ^/?([^/]*\.html?|[^\./]*)[:;,\.]*$ /$1 [R,L,NS]
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^/?([^/]*\.html?|[^\./]*)[:;,\.]*$ /foo/show.php [L,NS]Examples:
http://tomclegg.net/w/rewriterule
http://tomclegg.net/w/rewriterule.html;

R.K
0
 
LVL 3

Author Comment

by:jsev1995
Comment Utility
I want to redirect

http://example.com/jobtracker

to

http://example.com/jobtracker/

That's all. Could you just write a .htaccess for that? Thanks again.
0
 
LVL 23

Expert Comment

by:rama_krishna580
Comment Utility
Hi,

Many experts recommend to set up a 301 redirect (permanent redirect) from YourSite.com to www.YourSite.com...

RewriteEngine On
RewriteCond %{HTTP_HOST} ^YourSite\.com [nc]
RewriteRule (.*) http://www.YourSite.com/$1 [R=301,L]

Replace "YourSite.com" with your real domain name.

More info.
http://corz.org/serv/tricks/htaccess2.php
http://www.webconfs.com/how-to-redirect-a-webpage.php

R.K
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Author Comment

by:jsev1995
Comment Utility
read my question carefully

I tried that, but http://mysite.com/jobtracker is still giving a 400 bad request error. Here is what I want you to do:

Write a .htaccess file that redirects jobtracker to jobtracker/

Post it here

That is all I want you to do and I am not interested in anything else, at all. I do not want to know anything else about .htaccess or how to do other things. That is all I want. Nothing else. So please
0
 
LVL 13

Expert Comment

by:AngryBinary
Comment Utility
You mean you don't want a meandering lesson on regular expressions and Apache? Fine...

RewriteEngine On
RewriteCond %{THE_REQUEST} jobtracker$ [NC]
RewriteRule .* http://mysite.com/jobtracker/

My host already handles the 'missing slash' case, so I'm not able to verify that this rule doesn't fail. It looks right to me though... anyone able to see a problem with it?

--Randall
0
 
LVL 3

Author Comment

by:jsev1995
Comment Utility
mine does too, which puzzles me. It only happens on this directory. I am using cakePHP inside this directory, if that helps anybody.

Thanks, you did exactly what I wanted, but it didn't work. Anyhow, .htaccess file from cakePHP:

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    (.*) app/webroot/$1 [L]
</IfModule>

this is most likley causing the problem, please help. Thanks.
0
 
LVL 13

Expert Comment

by:AngryBinary
Comment Utility
That particular mod_rewrite directive will short-circuit anything that comes after it. The [L] tells apache to stop rewriting if it finds a match. So, you'll need to stick your custom directives before these lines. What this next deal essentially does is look for 'jobtracker' and the end of the request ($), and replaces only that portion of the request with jobtracker/.

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule    /jobtracker$ /jobtracker/ [NC]
   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    (.*) app/webroot/$1 [L]
</IfModule>

Again, I apologize if this doesn't work out exactly as planned, as I have no way to verify it on my own host.

--Randall
0
 
LVL 3

Author Comment

by:jsev1995
Comment Utility
I am sorry but none of your solutions worked, so here is what I'll do:

I am going to give out a separate URL which redirects to jobtracker/

I have notified community support and requested that this question be deleted.

AngryBinary,

I thank you for your efforts, you are the only one who gave my what I wanted, but it didn't work.
0
 
LVL 3

Author Comment

by:jsev1995
Comment Utility
community support notified.
0
 
LVL 1

Accepted Solution

by:
DarthMod earned 0 total points
Comment Utility
PAQd, 500 points refunded.

DarthMod
CS Moderator
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Accessibility and Usability are two concepts that seem to be closely related.  But, too many people seem to have a distorted perception of them. During last five years, those two words have come to the day-to-day work of almost every web develope…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now