View Full Version : Resolved .htaccess redirect
james438
01-05-2012, 10:32 AM
Is there a way to use htaccess or something else to redirect visitors to a certain default file if the folder is correct, but the filename is wrong?
For example, if a person tries to visit /admin/nonsense.php which does not exist they will be redirected to /admin/index.php.
baconDelta
01-05-2012, 11:13 PM
yes in your htaccess file you can write something like:
RewriteEngine On
RewriteRule ^/admin/nonsense.php$ /admin/index.php [L]
although mod rewrite is tricky. for instance this will redirect the user to index.php, but it won't change the url.
i would just put a header function in nonsense.php to redirect them to index.php, unless you have some huge amount of links you'd have to change, and tons of traffic would hit the nonsense.php page, then mod rewrite would be justified.
i'm not sure about this but i think the mod rewrite way would use less bandwith than the header route.
My first instinct was fine, but see post #6 for the version that actually works. :D
My first instinct is
#RewriteCond %{REQUEST_URI} (.*/).* !-d !-f [NC]
#RewriteCond %1 -d [NC]
#RewriteRule .* http://www.example.com/%1/index.php [L,R=301]
...but I'm not sure, and I'm not anywhere I can test it. Go ahead and try it if you like; I'll test it out later and report back.
(I would also assume that you would want the user's address bar to change, to alert them that they may have made a mistake.)
james438
01-06-2012, 10:37 AM
It does seem like that is close to working traq, because I am getting an internal server error when I type in a made up filename, but I also get it when I go to /admin/index.php as well.
admin is a fake folder by the way. It is definitely the folder that the hackers have been trying to hit though, so I want them to believe it is still a real folder while they continue to try to gain access to it.
It does seem like that is close to working traq, because I am getting an internal server error when I type in a made up filename, but I also get it when I go to /admin/index.php as well.
that just means that I made a syntax error somewhere.
I haven't done testing yet (sorry) but I'll let you know.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/[^/]
RewriteRule ^(.*)$ http://example.com%1/ [L,R=301]
this has the (unintended, but cool) consequence of working recursively. for example, say a user enters a URL like so:
http://example.com/good/url/nonsense/morenonsense/wth/typo.bleh
.htaccess will rewrite it like so:
example.com/good/url/nonsense/morenonsense/wth/
which will be rewritten like
example.com/good/url/nonsense/morenonsense/
and so on, until we get to
example.com/good/url/
cool, huh?
the only caveat is that /good/url/ must contain an index file, or, at this point, you'll get a 404.
james438
01-07-2012, 05:36 AM
Cool htaccess trick :) It works exactly like I had hoped.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.