1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Mod_Rewrite Expert needed

Discussion in 'Hire a Freelancer' started by subster, May 31, 2012.

  1. subster

    subster Elite Member

    Joined:
    Apr 5, 2008
    Messages:
    1,882
    Likes Received:
    1,454
    Location:
    Krauthausen
    Hi there,

    I am currently migrating to a new shop and every url switches to a new one.
    I've this gathered and wanted to do it with a simple redirect 301, but unfortunately due to some mod_rewrite rules in the htaccess it wont work.

    Problem:

    This block of rules is crucial for the script to work properly:

    Code:
    	#RewriteRule ^(.*)(.+)\.html(.*)$ 								commerce_seo_url.php?linkurl=$1$2$3 [qsappend,L]
    
    	RewriteCond %{REQUEST_FILENAME} !\.html$ [OR]
    	RewriteCond %{REQUEST_FILENAME} !\.php$ [OR]
    	RewriteRule ^(.*)$												commerce_seo_url.php?linkurl=$1 [qsappend,L]
    
    If I paste now my redirects into the htacess the redirect works, but the targeted url gets an ?linkurl=/old/category at the end what leads the redirect into an 404.


    Explained Example:

    Pasted at the end of .htaccess:

    redirect 301 /old/categorystructure http://www.domain.com/new/categorystructure/ (the old urls did not have a backslash at the end)

    Result:
    Code:
    http://www.domain.com/new/categorystructure?linkurl=/alter/ordner
    = 404 error

    I search someone who could find the right syntax of a mod_rewrite rule to work this problem out.

    It is urgent.

    Thank you for your replies.
     
  2. subster

    subster Elite Member

    Joined:
    Apr 5, 2008
    Messages:
    1,882
    Likes Received:
    1,454
    Location:
    Krauthausen
    anyone? quick dollars to be made! you show me the working rule and I will pay for it.
     
  3. sockpuppet

    sockpuppet Junior Member

    Joined:
    Nov 7, 2011
    Messages:
    155
    Likes Received:
    145
    put your redirects at the beginning of the htaccess file

    if your old path doesn't end with .html or .php this rule gets executed which puts the linkurl=... at the end
    Code:
    	RewriteRule ^(.*)$           commerce_seo_url.php?linkurl=$1 [qsappend,L]
    
     
    • Thanks Thanks x 1
  4. subster

    subster Elite Member

    Joined:
    Apr 5, 2008
    Messages:
    1,882
    Likes Received:
    1,454
    Location:
    Krauthausen
    Thank you so much for your reply!
    I pasted them into nearly every possible position.

    Old urls doesnt have .html / .php endings and with the linkurl= suffix the redirects are leading into a 404...
     
  5. subster

    subster Elite Member

    Joined:
    Apr 5, 2008
    Messages:
    1,882
    Likes Received:
    1,454
    Location:
    Krauthausen
    I got a bit deeper into this issue:

    the suffix ?linkurl= comes from the qsappend from this line:

    Code:
    RewriteRule ^(.*)$ commerce_seo_url.php?linkurl=$1 [qsappend,L]
    I am damnsure the script needs this commad to work properly, so my only chance is to use [L,301] somewhere before this line like

    Code:
    RewriteRule ^old/(.*) http://example.com/new/$1/ [L,301]
    But this results in an 500 error. Man, the whole day is going up in flames on this issue... frustatring..
     
  6. goodfor

    goodfor Newbie

    Joined:
    May 26, 2012
    Messages:
    8
    Likes Received:
    1
    I understand your frustration. I wish I could help. I know this comment seems worthless to you BUT it might make you feel a lil better knowing that my day, too, has gone up in flames. Good luck, man.
     
    • Thanks Thanks x 1
  7. sockpuppet

    sockpuppet Junior Member

    Joined:
    Nov 7, 2011
    Messages:
    155
    Likes Received:
    145
    [301] is not a valid flag, that is why you get a 500 error

    it should be something like this
    Code:
    RewriteRule ^old/(.*) http://example.com/new/$1/ [QSA,R=301,L]
    
     
    • Thanks Thanks x 1
  8. -Jericho-

    -Jericho- Jr. Executive VIP

    Joined:
    Jan 10, 2010
    Messages:
    2,850
    Likes Received:
    1,708
    Location:
    Stalking My Ex-Wife
    If you're still having troubles go to http://stackoverflow.com/

    I always get great responses there fairly quickly. They've helped me solve a number of programming issues.
     
    • Thanks Thanks x 1
  9. subster

    subster Elite Member

    Joined:
    Apr 5, 2008
    Messages:
    1,882
    Likes Received:
    1,454
    Location:
    Krauthausen
    Thanks for the headsup goodfor :)

    Thanks for this final tip sockpoppet - Rep and thanks given!

    One final question, If you'd be so kind to answer me this last one...

    Code:
    RewriteRule ^cat1/subcat1/(.*) http://example.com/new/$1/ [QSA,R=301,L]
    works great for all my categories. But when I add a products url to redirect it give the 404 again. Where is the failure here:

    Code:
    RewriteRule ^cat1/subcat1/product-url-without-trailing-slash-or-other-ending/(.*) http://unchangedurl.com/cat1/newsubcat$1/ [QSA,R=301,L]
    Additional question: I have to creat this rules for roundabout 5K product urls, will the performance get a hard hit by that?

    Thanksthanksthanks - I am really greateful for your help and sharing your knowledge.
     
  10. Zapdos

    Zapdos Power Member

    Joined:
    Oct 22, 2011
    Messages:
    597
    Likes Received:
    708
    Location:
    Eastern North Carolina
    Every htaccess rule decreases performance.
     
  11. sockpuppet

    sockpuppet Junior Member

    Joined:
    Nov 7, 2011
    Messages:
    155
    Likes Received:
    145
    Do you add the product url after your "category-rule"? that's the problem.
    Your product url get matched by the "category-rule": "^cat1/subcat1/(.*)", so it never reaches the redirect for the product url.

    The problem with .htaccess files they get reread by apache on every request, if you have a high traffic site even small files could hit on the performance. So i wouldn't recommend a file with 5k redirects. You could put the redirects in your apache config, so they get read once.
    I don't know how your urls look like, but the best way is to avoid the 5k redirects and find a rule that matches all of your product urls. If that is not possible you should use a php script for your redirects, i think that would be faster than mod_rewrite.
     
    • Thanks Thanks x 1
  12. subster

    subster Elite Member

    Joined:
    Apr 5, 2008
    Messages:
    1,882
    Likes Received:
    1,454
    Location:
    Krauthausen
    The Solution:


    Catgoryurls

    Code:
    RewriteRule ^/maincat/subcat/? http://domain.de/neuemaincat/neuesubcat/ [R=301,L]
    Producturls

    Code:
    RewriteRule ^maincat/subcat/produkturl/?$ http://domain.de/neuemaincat/neuesubcat/produkturl.html [R=301,L]


    just if someone else is in the same need...