Solved

400 Bad Request

Posted on 2006-11-04
15
443 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
ID: 17874619
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
ID: 17874659
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
ID: 17876143
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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
LVL 3

Author Comment

by:jsev1995
ID: 17876185
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
ID: 17876467
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
ID: 17876492
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
ID: 17877211
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
 
LVL 3

Author Comment

by:jsev1995
ID: 17877406
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
ID: 17885656
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
ID: 17885870
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
ID: 17885968
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
ID: 17910325
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
ID: 17910342
community support notified.
0
 
LVL 1

Accepted Solution

by:
DarthMod earned 0 total points
ID: 17964675
PAQd, 500 points refunded.

DarthMod
CS Moderator
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

840 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