We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


400 Bad Request

jsev1995 asked
Medium Priority
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
Watch Question


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.


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.

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?
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:
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.



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.

look at here...

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:

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
RewriteRule ^/?([^/]*\.html?|[^\./]*)[:;,\.]*$ /$1 [R,L,NS]
RewriteRule ^/?([^/]*\.html?|[^\./]*)[:;,\.]*$ /foo/show.php [L,NS]Examples:



I want to redirect




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

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.



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
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?



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]

this is most likley causing the problem, please help. Thanks.
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]

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



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.


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


community support notified.
PAQd, 500 points refunded.

CS Moderator

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.