SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Broken rewrite help

  1. #1
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Broken rewrite help

    Below is something I had a coder do for me. They have since gone mia and I am stuck trying to figure this out myself.

    It is for a site I am working on. The site is almost done but these redirects make the site painfully slow and the js and css file stop loading. I know there is something wrong - can't figure out exactly what it is.

    In the end I am trying to get...

    1
    domain.com/games-A.html to redirect to => domain.com/games/a
    domain.com/games-B.html to redirect to => domain.com/games/b
    And so on... all the way to z.

    2
    domain.com/4578/air-tales.html to redirect => domain.com/games/a/air-tales
    domain.com/654646577/blood.html to redirect => domain.com/games/b/blood

    3
    domain.com/games-num.html to redirect to => domain.com/games/0-9

    4
    domain.com/46544/2006-fifa-world-cup.html to redirect to => domain.com/games/0-9/2006-fifa-world-cup

    I'd really appreciate it if someone can point me in the right direction.

    Code:
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_URI} !=/favicon.ico
      RewriteRule ^games-(.*).html$ /games/$1 [L,QSA,NC,R=301]
      RewriteRule ^(.*)/([a-z])(.*).html$ /games/$2/$2$3 [L,QSA,NC,R=301]
      RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
    Thanks
    Always buying established websites and premium domains.

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

    Hmmm, some comments are in order:

    1. The "direction" of the redirect IS important. You state that you want to redirect FROM a URI that Apache can serve to something that it can't. If you want to display the "something that can't" format URI, then you must redirect from that TO something Apache can serve. Because the final line of your .htaccess makes it appear to be WP code, you may be able to ignore this.

    2. .js, .css, .jpg, et al support file links are generally the result of changing the directory (level) of the URI in the redirection. See my signature's tutorial article under Missing Support Files for the two solutions.

    3. I think your "coder" is not MIA but hiding in shame! That looks like the type of @#$% that online scripts will build for free (and generally work as well).
    In the end I am trying to get...

    1
    domain.com/games-A.html to redirect to => domain.com/games/a
    domain.com/games-B.html to redirect to => domain.com/games/b
    And so on... all the way to z.
    The problem here is changing an upper case character to its lower case counterpart. Do you have access to the httpd.conf? If not, you won't have Apache's {toupper} function so you're stuck with a series of mod_alias or mod_rewrite statements.
    2
    domain.com/4578/air-tales.html to redirect => domain.com/games/a/air-tales
    domain.com/654646577/blood.html to redirect => domain.com/games/b/blood
    There is NO way for Apache to GUESS that b translated to 654646577. IMHO, that's not required, though, as you should be able to change the blood.html's underlying script (you ARE database driven, aren't you?) to fetch the blood field's name rather than the id.
    3
    domain.com/games-num.html to redirect to => domain.com/games/0-9
    There is no way for Apache to know that num = 0-9, is there? Okay, that can be hard-coded but what are the other options?
    4
    domain.com/46544/2006-fifa-world-cup.html to redirect to => domain.com/games/0-9/2006-fifa-world-cup
    Again, where in the world is Apache supposed to get 46544 from?
    I'd really appreciate it if someone can point me in the right direction.
    READ the mod_rewrite tutorial article linked in my signature - with emphasis on the Regex section.
    Code:
      # Start properly
      RewriteEngine on
      # If not a file
      RewriteCond %{REQUEST_FILENAME} !-f
      # AND if not a directory
      RewriteCond %{REQUEST_FILENAME} !-d
      # AND if not favicon.ico (=/ is dead wrong!)
      RewriteCond %{REQUEST_URI} !=/favicon.ico
      # Redirect FROM games-{zero to many of any character}.html
      # to games/{zero to many of any character}
      # Last means to terminate the mod_rewrite block,
      # QSA is to retain any query string,
      # NC (No Case) is irrelevant (TOTALLY worthless here) and
      # R=301 says to make this a permenant redirection (SE's and display)
      RewriteRule ^games-(.*).html$ /games/$1 [L,QSA,NC,R=301]
    
      # Capture {zero to many  of any character}/{one lower case character}
      # -> {a different zero to many}.html and redirect to 
      # -> games/{sincle character}/{single character}{a different zero to many}
      # -> with the same ridiculous set of flags
      RewriteRule ^(.*)/([a-z])(.*).html$ /games/$2/$2$3 [L,QSA,NC,R=301]
    
      # THEN redirect EVERYTHING to index.php as the value for q!
      RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
    This has the sick flavor of WP but modified to be absurd. My experience with WP is that you don't want to mess with it because the changes they make to the URI (and fed to index.php) are handled by WP modules. Changing the expected input to those modules can destroy the entire WP installation because it's not designed to deal with @#$%.

    Regards,

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

  3. #3
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the detailed reply... Using your site and a few others, I was able to hack together something that works but alas drupal doesn't play nice with it.

    Have to figure something else out.
    Always buying established websites and premium domains.

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
  •