Results 1 to 8 of 8

Thread: .htaccess redirect

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default .htaccess redirect

    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.
    Last edited by james438; 01-07-2012 at 09:44 AM.
    To choose the lesser of two evils is still to choose evil. My personal site

  2. #2
    Join Date
    Dec 2011
    Posts
    49
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default

    yes in your htaccess file you can write something like:

    PHP Code:
    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.
    Last edited by baconDelta; 01-05-2012 at 11:14 PM. Reason: typo

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Edit: My first instinct was fine, but see post #6 for the version that actually works.


    My first instinct is
    Code:
    #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.)
    Last edited by traq; 01-07-2012 at 03:29 AM.

  4. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    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.
    To choose the lesser of two evils is still to choose evil. My personal site

  5. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by james438 View Post
    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.

  6. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

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

  7. The Following User Says Thank You to traq For This Useful Post:

    james438 (01-07-2012)

  8. #7
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Cool htaccess trick It works exactly like I had hoped.
    To choose the lesser of two evils is still to choose evil. My personal site

  9. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •