SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jan 2014
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Folder files redirect correctlyto a file EXCEPT if there is a "?" character in the ur

    Hi,

    Goal:
    301 redirect all requests for files in an old folder to one file in another folder using htaccess. That is: /forum/everypossiblefile --> /faq/index

    Should be a very easy to find fix. 1) not so easy to find. 2) found two solutions that work...unless there is a "?" in the url. About 70% of the 1000 or so urls in our defunct forum had "?" somewhere. Thus,

    /forum/index.php redirects correctly to
    /faq/

    /forum/viewforum.php?f=5 redirects to the made up URL
    /faq/?f=5

    Oddly, perhaps because of the same or other magical quality of “?” in an URL that is causing the problem, all variations of /faq/?whatever resolve correctly to the one target file at /faq/index.htm.

    We don’t want to just call this good, because besides readers it’s mainly robots we are trying to redirect.


    I used the tricks suggested at: http://www.sitepoint.com/forums/show...%28htaccess%29

    RewriteRule ^/?australien_faq/(.*)$ http://www.ausmag.de/australien-faq/ [R=301,L]

    and also tried from http://stackoverflow.com/questions/7...-a-single-file

    RedirectMatch 301 ^/myDir/(.*) http://www.mydomain.com/myNewDir/

    Again, they both worked great except for the “?” issue. Thanks for any help!
    Last edited by Mittineague; Jan 15, 2014 at 17:57. Reason: OP forgot to turn off auto-link

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,892
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    The trick is to tell Apache to remove any query string, which you can by adding a ? to the URL to be redirected to. Put this in your .htaccess

    Code:
    Options +Symlinks -MultiViews
    RewriteEngine On
    RewriteRule ^forum/(.*)$ /faq/? [L,R=301]
    This will not redirect to /faq/?, but to /faq/, and remove any query strings in the process (like ?f=5).
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Member
    Join Date
    Jan 2014
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for this quick response! It's almost 2 am here, so I'm reluctant to try this (and maybe break the whole site just before going to bed).
    Just to be sure I've got the syntax correct, would this be it?

    RewriteRule ^/oasisdesign.net/forum/(.*)$ http:// oasisdesign.net/faq/? [R=301,L]

    Detail questions--
    1) what does Options +Symlinks -MultiViews do? Is it necessary?
    2) ReWrite engine is already turned on earlier in the page...leave this out, or keep turning it on over and over again for each of these commands (there are several similar ones)
    3) An earlier poster said the absolute link did the trick for them...any reason to have it something else vs absolute?

    thanks again,

    Art

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,604
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    od,

    The "problem" is that query strings are handled separately from the {REQUEST_URI} string which RewriteRules operate on. Please note that the "?" character is reserved in order to denote the separation from the URI and query string.

    Rémon's solution of stripping the query string by redirecting to a URI with a trailing ? will DELETE any pre-existing query string which would otherwise be automatically passed through. If you need to create a new key/value pair in your existing query string (which you're not doing here), you will need the QSA (query string appended) flag.

    Simple problem, simple solution.

    As for your questions:

    Detail questions--
    1) what does Options +Symlinks -MultiViews do? Is it necessary?

    Those should already be set in the server's httpd.conf file but they are necessary for mod_rewrite to work. Remove them to test whether your host is "on the ball" or not - you can always replace them.

    2) ReWrite engine is already turned on earlier in the page...leave this out, or keep turning it on over and over again for each of these commands (there are several similar ones)

    mod_rewrite can be put in a "comment mode" using RewriteEngine off and returned with RewriteEngine on. It's simply considered "good form" to start your mod_rewrite code with RewriteEngine on to ensure that it's not been put in comment mode elsewhere.

    3) An earlier poster said the absolute link did the trick for them...any reason to have it something else vs absolute?

    It should make NO difference as mod_rewrite will operate on the Apache variables separately.

    You might benefit from reading the mod_rewrite tutorial linked in my signature as it contains explanations and sample code. It's helped may members and should help you, too.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    SitePoint Member
    Join Date
    Jan 2014
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Scallio, DK...

    I appreciate your help very much yet am in far enough over my head that I can't understand much of what I'm sure is a helpful response that contains the answer to my issue.

    I tried both

    Options +Symlinks -MultiViews
    RewriteEngine On
    RewriteRule ^forum/(.*)$ /faq/? [L,R=301]


    and

    Options +Symlinks -MultiViews
    RewriteEngine On
    RewriteRule ^/oasisdesign.net/forum/(.*)$ http:// oasisdesign.net/faq/ [R=301,L]


    and what I got was basically the comic book not good result, which was this for every single page on the site (ok, two or three of the outdated URLs, the faq and the home page.).
    Is it possible to tell from the code I've posted above what went wrong, or more importantly, what would work?


    Internal Server Error

    The server encountered an internal error or misconfiguration and was unable to complete your request.

    Please contact the server administrator, webmaster@oasisdesign.net and inform them of the time the error occurred, and anything you might have done that may have caused the error.

    More information about this error may be available in the server error log.

    Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.


    I have successfully added a bunch of other stuff to the .htaccess file that has been functioning well. I put a copy of it here temporarily.

    Thanks again,

    Art


Tags for this Thread

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
  •