301's, mod_rewrites, the works :)

Discussion in 'Apache' started by J8Diamonds, Nov 9, 2007.

  1. #1
    Okay, so after spending about three days trying to figure out mod_rewrite, I'm pretty sure I've gone in the wrong direction :( On the plus side I now kind of understand mod_rewrite.

    I'm trying to 301 redirect a dynamic url of the form www.domain.com/index.php?pg=785 to www.domain.com/topic/optional/optional

    Now as far as I can tell mod_rewrite will only allow me to change www.domain.com/index.php?pg=785 to www.domain.com/785, which unfortunately is not what I want :(

    Can anybody tell me how (or at least show me the right direction) to point my different page ids to a new static url?

    *edit*Also I need to change sub.domain.com/index.php?pg=786 to go to sub.domain.com/topic/optional/optional, because I imagine that'll be slightly different too.*edit*

    All help appreciated.
     
    J8Diamonds, Nov 9, 2007 IP
  2. J8Diamonds

    J8Diamonds Active Member

    Messages:
    354
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #2
    J8Diamonds, Nov 9, 2007 IP
  3. Ladadadada

    Ladadadada Peon

    Messages:
    382
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If you have a small list of redirects/rewrites you want then you could just each one on it's own line.
    RewriteRule ^/index.php?pg=785 /topic/optional/optional [R=301,NC,QSA,L]
    RewriteRule ^/index.php?pg=786 /topic/optional/optional2 [R=301,NC,QSA,L]
    Code (markup):
    However I don't think that's what you're actually asking for.

    If you want a rewrite rather than a redirect, you can have the URLs displayed in your user's browsers look like /topic/optional/optional. The trouble is, Apache can't do this alone unless you give it all the information it needs. This means adding one line for every separate URL -> pg code lookup you have. This is obviously less than ideal. On the other hand... if you want to use PHP as well, then it becomes much easier.

    The way I have achieved this is either for my blog URLs:
    RewriteRule ^/blog/([0-9]{4}/[0-9]{2}/[0-9]{2}) /blog/index.php?date=$1 [QSA]
    Code (markup):
    Then I simply use PHP to convert the date into a timestamp and look it up in the database.

    For my article URLs:
    RewriteRule ^/articles/([a-zA-Z0-9_-]+)$ /articles/index.php?articlecode=$1 [QSA,L]
    Code (markup):
    I use pretty much the same methodology except that I use the text part of the URL as the lookup rather than the date.

    For your situation, I would probably use something like:
    RewriteRule ^/[a-z]+(/[a-z]+)*/([0-9])+ /index.php?pg=$2 [NC,QSA,L]
    Code (markup):
    which does not exactly match the URLs you specified but instead has the pg code appended to the end. This way you have a nice, easy integer to look up in your database.
     
    Ladadadada, Nov 10, 2007 IP
  4. J8Diamonds

    J8Diamonds Active Member

    Messages:
    354
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Okay, I have a couple of smaller sites that are only 10-15 pages that I'm moving as well, so I thought I'd try your first suggestion.

    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    
    RewriteRule ^/index.php?pg=8 /index.html [R=301,NC,QSA,L]
    Code (markup):
    Which didn't work, so I then tried to put the URL in full

    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    
    RewriteRule ^/index.php?pg=8 http://www.mydomain.com/index.html [R=301,NC,QSA,L]
    Code (markup):
    Which also didn't work. Am I missing something?

    The rewrite engine is definately on.
     
    J8Diamonds, Nov 12, 2007 IP
  5. Ladadadada

    Ladadadada Peon

    Messages:
    382
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If you're putting this in a .htaccess file, you need to remove the leading slash from the regex part of the rewrite rule.

    RewriteRule ^index.php?pg=8 /index.html [R=301,NC,QSA,L]
    Code (markup):
    Although that rewrite looks kinda backwards to me. It would be more normal to have:

    RewriteRule ^index.html /index.php?pg=8 [NC,QSA,L]
    Code (markup):
    Which would leave index.html in the URL bar of the browser but display the content from index.php?pg=8.
     
    Ladadadada, Nov 22, 2007 IP