mod_rewrite REVERSED

Discussion in 'Apache' started by lilyvo30, Aug 4, 2010.

  1. #1
    I have been trying for DAYS to get a RewriteRule to work on my site and every time I figure it out it works reverse (in other words it generates a new url that directs back to the old url.

    2 reverse examples:

    RewriteRule ^([^/]+)/([^/]+)\.html$ pages/companyprofile.php?recordID=$1/$2 [R=301]

    RewriteRule test(.*)\.htm$ /pages/companyprofile.php?recordID=$1 [R=301,L]

    The link that I want to rewrite is (where 7 and voiparty are automatically generated):
    mysite.../pages/companyprofile.php?recordID=7/voiparty

    End result being
    mysite.../7/voiparty.html

    If there is any way that you can help me that would be amazing. I am obviously missing something. Common sense tells me just to switch the 2 but it always results in errors. I NEED YOUR HELP!!!! THANK YOU SO MUCH IN ADVANCE!
     
    lilyvo30, Aug 4, 2010 IP
  2. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #2
    RewriteEngine on
    RewriteRule ^(\d+)/(.+)\.html$ pages/companyprofile.php?recordID=$1/$2 [L]
    
    Code (markup):
    Should do it, that's if I understand what you want.

    As you have had it redirecting you might find it still redirects in which case you need to clear your browser cache.
     
    tolra, Aug 5, 2010 IP
  3. lilyvo30

    lilyvo30 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Tolra, thank you so much for your help I put that in my .htaccess file and it does the same thing the others are doing. Without putting R=301 as a flag it shows up as a direct link but the old link doesn't redirect to it. So I then put [R=301, L] and the newly generated link redirects to the old one. From what I understand a RewriteRule should take the old link and redirect to the new link. Do you have any idea or suggestions of what I am doing wrong? THanks again!
     
    lilyvo30, Aug 5, 2010 IP
  4. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #4
    With the [R] it will redirect, I thought you wanted to just call the script on passing parameters based on the URL.

    Can you post an example of the URL typed into the address bar and an example of the URL that will be redirected to, that way I hope I can understand were you are trying to get to. If there's more than one case please give me examples of each.
     
    tolra, Aug 5, 2010 IP
  5. lilyvo30

    lilyvo30 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I'm sorry, I am new at all of this. Again, thanks for your help!

    The URL that I have now is www mlmrank com/pages/companyprofile.php?recordID=7/voiparty
    The URL that I want it to redirect is www mlmrank com/7/voiparty.html
     
    lilyvo30, Aug 6, 2010 IP
  6. lilyvo30

    lilyvo30 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    What I have gotten so far is the newly generated link that I want but with the 301 redirect it is redirecting me back to the old URL, I need the old url to redirect to the new url. I left the code in that you gave me so you can see if you go to my new url that it redirects to the old one (with R=301 in the code). THANK YOU TOLRA!!! I am so grateful for your help!
     
    lilyvo30, Aug 6, 2010 IP
  7. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #7
    Remove the redirect I gave you before from .htaccess, then put a .htaccess in your pages folder:
    RewriteEngine on
    RewriteCond %{REQUEST_URI} ^/pages/companyprofile\.php$
    RewriteCond %{QUERY_STRING} recordID=(\d+/.+)
    RewriteRule .* http://www.mlmrank.com/%1.html? [R=301]
    
    Code (markup):
    That redirects the ?recordID=7/voiparty version to 7/voiparty.html version.
     
    tolra, Aug 6, 2010 IP
  8. lilyvo30

    lilyvo30 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    So I did what you said. I removed the code that you gave me originally and created a new htaccess file in my pages folder. Then I put the code above in it. It does redirect to the link that I want but I get a 404 not found error. I am going to leave it up (I can't leave it up for long but I will try so you can see). I am guessing the page is redirecting to a link that hasn't been created? I really am not sure though. Tolra, your help is SOOOOO much appreciated! What are your thoughts?
     
    lilyvo30, Aug 6, 2010 IP
  9. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #9
    You don't appear to have a file or script for /7/voiparty.html so as there's nothing there you get the 404.

    Off the top of my head, in your top level .htaccess put:
    RewriteEngine on
    RewriteRule ^(\d+)/(.+)\.html$ pages/companyprofile.php?recordID=$1/$2 [L]
    Code (markup):
    Put the .htaccess from above in your pages folder and see what happens, it might loop horribly and crash but give it a try. If it doesn't work I'll have another look at the rules as I understand what you are trying to do now.
     
    tolra, Aug 6, 2010 IP
  10. lilyvo30

    lilyvo30 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I added that code to my top level .htaccess file and when I go to one of the links I get a warning saying the page isn't redirecting in a way that it will never complete. I am guessing that is a looping problem?
     
    lilyvo30, Aug 7, 2010 IP
  11. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #11
    Yes, that's looping, I'll have a think later once I get a job finished.
     
    tolra, Aug 7, 2010 IP
  12. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #12
    In your root .htaccess
    RewriteEngine on
    RewriteRule ^(\d+)/(.+)\.html$ pages/companyprofile.php?recordID=$1/$2 [L]
    
    Code (markup):
    In the .htaccess in pages folder
    
    RewriteCond %{THE_REQUEST} !^[A-Z]+\ /\d+/.+\.html\ HTTP/
    RewriteCond %{REQUEST_URI} ^/pages/companyprofile\.php$
    RewriteCond %{QUERY_STRING} recordID=(\d+/.+)
    RewriteRule .* http://www.mlmrank.com/%1.html? [R=302]
    
    Code (markup):
    It's working on my test server but let me know how you get on with it.
     
    tolra, Aug 7, 2010 IP
  13. lilyvo30

    lilyvo30 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    YOU ARE AMAZING!!!!!! I AM SO GRATEFUL FOR ALL OF YOUR HELP! I am basically in tears right now. Thank you so much, everything is working exactly as I hoped. THANK YOU!!!!!!!!
     
    lilyvo30, Aug 7, 2010 IP
  14. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #14
    You're welcome, glad we got there in the end :)
     
    tolra, Aug 7, 2010 IP