FAQ: mod_rewrite, 301 redirects, and optimizing Apache.

Discussion in 'Apache' started by Nintendo, Jul 30, 2005.

  1. danzor

    danzor Peon

    Messages:
    208
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #441
    Guess i'm toast then.. :(


    http://www.mardy-bum.com/index.php?page=music should redirect to http://www.mardy-bum.com/site/music/. Although it seems to be a bit non functional at the moment.

     
    danzor, Dec 11, 2006 IP
  2. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #442
    Try

    RewriteCond %{QUERY_STRING} ^page=[^&]+)?$
    RewriteRule ^index\.php?page=([^.]+)$ http://www.domain.com/site/$1 [R=301,L]

    A code that I've used that works is...

    RewriteCond %{QUERY_STRING} ^t=23044(&page=[^&]+)?$
    RewriteRule ^showthread\.php$ http://forums.digitalpoint.com/showthread.php?t=666 [R=301,L]

    but that was for just one thread in a vBUlletin board. For example, this thread and all it's pages. The code being if this thread moved to showthread.php?t=666
     
    Nintendo, Dec 11, 2006 IP
  3. danzor

    danzor Peon

    Messages:
    208
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #443
    I got an error when I tried that:
    and from looking at the other example I assumed you missed out the beginning (, so I put that back in and i'm still not getting anything unfortunately.
    The rest of my url rewrites are working fine, though.

     
    danzor, Dec 12, 2006 IP
  4. isildur

    isildur Peon

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #444
    Ok. I have called my buddies (all two of them). I have looked in an Apache book. I have Googled it. Finally, I sacrificed a chicken on the keyboard. I still cannot solve this stupid redirect problem I have been having.

    Someone save me.

    I just want to redirect all traffic from an old non-functioning URL to a new subdomain:

    from this URL: http://geekz.us/?q=arsfiles

    to this one: http://arslist.geekz.us

    I have tried the following to no avail:

    Redirect ^http://geekz.us/?q=arsfiles$ http://arslist.geekz.us [r]

    Redirect /\?q=arsfiles http://arslist.geekz.us

    Redirect /?q=arsfiles http://arslist.geekz.us

    RewriteRule ^$ /?q=arsfiles

    RewriteBase /
    RewriteRule ^/?q=arsfiles$ http://arslist.geekz.us

    So I am pretty sure my google-fu and my .htaccess-fu are both The Suck(C).

    Anyone shed some light here in this darkness?

    Thanks!
     
    isildur, Dec 15, 2006 IP
  5. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #445
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^index.php?q=(.*)$ http://arslist.geekz.us [R=301,L]

    with index.php being what ever the script name is.
     
    Nintendo, Dec 15, 2006 IP
  6. isildur

    isildur Peon

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #446
    Almost. See, my original url was http://geekz.us/?q=arsfiles since I was using Drupal. I have since stopped using that CMS and am moving the content the old url pointed at to a subdomain. I still have www.geekz.us which has other content that I still want users to reach.

    When I tried your solution, I got an internal server error on http://www.geekz.us and on http://arsfiles.geekz.us
    Of course, this is farther than I have gotten yet :)

    Since there was no original script like index.php? i just tried this:

    RewriteRule ^/?q=arsfiles(.*)$ http://arslist.geekz.us/ which did not work.

    Any other ideas?

    PS Thanks for the response Nintendo, I appreciate it.
     
    isildur, Dec 16, 2006 IP
  7. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #447
    Do a test to see if you even got mod_rewrite.

    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^wacko\.html$ index.php [R=301,L]

    domain.com/wacko.html should show the same thing as the index page. Try it with out the first two lines if it doesn't work.
     
    Nintendo, Dec 16, 2006 IP
  8. isildur

    isildur Peon

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #448
    Yes, that works. I kind of new it worked because I already have another redirect setup like so:

    Redirect /ars.asp http://arslist.geekz.us

    I have been through three CMS's running this site. I used to have this in the file which worked perfectly:

    redirect permanent /ars.asp http://geekz.us/?q=arsfiles

    That was for my switch from static pages to the first CMS (Drupal). Now I am switching to Wordpress, but I want to redirect two links which I have used and been linked to by other sites so that folks hitting those links still get directed to the proper address.

    As of right now

    Redirect /ars.asp http://arslist.geekz.us

    works perfectly, and now I just need to get these two pointing to http://arslist.geekz.us without hosing the rest of the site:

    http://geekz.us/?q=arsfiles (Drupal)
    http://geekz.us/viewpage.php?page_id=46 (php-fusion)

    I see not why so many folks have been using URL rewriting. It sure makes things easier when you decide to change CMS's.

    Thanks for your help. I am a programmer/consultant by trade, but I cannot seem to wrap my brain around this .htaccess stuff. :eek:

     
    isildur, Dec 16, 2006 IP
  9. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #449
    Nintendo, Dec 16, 2006 IP
  10. isildur

    isildur Peon

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #450
    Too my knowledge, there wasn't a file. This was from the Drupal CMS which I used first. I thought it was wierd, but it is the way it was displayed in the browser. All my links internally were like that too. I still have some old backups of the entire site when it was running like that so I can still look at the old files, sql dumps, etc.

    Beats me. :eek:
     
    isildur, Dec 16, 2006 IP
  11. isildur

    isildur Peon

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #451
    isildur, Dec 16, 2006 IP
  12. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #452
    There has to be some file that's making that page. A page doesn't come out of nothing. There's no way to do mod_rewrite unless you know what it is.

    Look in the directory for something like index.XXX
     
    Nintendo, Dec 16, 2006 IP
  13. isildur

    isildur Peon

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #453
    Well. I agree that there is usually a page which references this. But I swear my links I left in forums (which worked fine) did not include a page. My only guess is that the parameters were passed to the index.php file which was live at that time, but is not now.

    I know it looks wierd, but http://geekz.us/?q=arsfiles always took you to the right page when I was using Drupal. I never liked it and never could get static links to work properly within the CMS.

    Is there anyway to redirect just using a substring like "arsfiles"? Or is there any type of aliasing that can be done? I am such an .htaccess noob.

    Thanks for the tips,
     
    isildur, Dec 16, 2006 IP
  14. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #454
    Nintendo, Dec 16, 2006 IP
  15. ian_ok

    ian_ok Peon

    Messages:
    551
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #455
    I'm having a change round in one of my sites and have the following to write database content in a more user friendly URL:

    RewriteRule ^property_forsale_details-([0-9]*).html property_forsale_details.html?listingID=$1

    However I'll be wanting to delete about 75% of these pages, so will I'll be able to add a redirect for the old pages after this rewrite rule or do I have to do it another way??

    Thanks Ian
     
    ian_ok, Dec 17, 2006 IP
  16. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #456
    If it's static URLs going to new URLs you should be able to redirect them.

    Redirecting dynamic URLs are more challenging.
     
    Nintendo, Dec 18, 2006 IP
  17. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #457
    crazyryan, Dec 20, 2006 IP
  18. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #458
    
    RewriteRule ^/user/(.*)/?$ /profile.php?name=$1
    
    Code (markup):
    :) :)
     
    krakjoe, Dec 20, 2006 IP
  19. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #459
    That didn't seem to work but my friend told me:
    RewriteRule ^user/([^/\.]+)/?$ profile.php?user=$1 [L]
    would.
     
    crazyryan, Dec 20, 2006 IP
  20. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #460
    he was wrong, send me some ftp access or try :

    RewriteRule ^user/(.*)/?$ /profile.php?name=$1

    is it ?user or ?name ?

    also, the full code should look something like

    
    RewriteEngine On
    RewriteBase /.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^/user/(.*)/?$ /profile.php?name=$1
    
    Code (markup):
    not just the one liner, you prolly got that but you never know who is reading ......
     
    krakjoe, Dec 20, 2006 IP