Announcement

Collapse
No announcement yet.

redirect .aspx with query string to html page using htaccess

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    redirect .aspx with query string to html page using htaccess

    We have migrated from a windows server to linux server. I trying to redirect all the .aspx urls to html urls.

    The static pages works fine so say if I used

    Code:
    Redirect 301 /contact.aspx http://mysite.com/contact.html
    but when I try to add a redirect like
    Code:
    Redirect 301 /products.aspx?cid=30&bid=5 http://mysite.com/category/books.html
    the redirect is not working.

    Any advice is highly appreciated
    Keyboard Not Found..... Press F1 to continue

    #2
    Re: redirect .aspx with query string to html page using htaccess

    The 'Redirect' and 'RedirectPermanent' commands only look at the page request, and ignore the query string. You'll want:

    Code:
    # Only needs to be used one time in .htaccess
    RewriteEngine On
    # Repeat the below as needed for other strings
    RewriteCond %{QUERY_STRING} cid=30&bid=5
    RewriteRule ^(.*)$ http://mysite.com/category/books.html? [R=301,L]
    The purpose of the question mark after books.html is to prevent the browser from throwing the original query string back on the new request, which would get it stuck in an infinite loop.
    Last edited by ILoveHostasaurus; 08-07-13, 10:34 AM.
    David Hubbard
    CIO
    Miva
    [email protected]
    http://www.miva.com

    Comment


      #3
      Re: redirect .aspx with query string to html page using htaccess

      Thanks a bunch, that works great. but now for some reason redirect non-www to www is not working. I am trying to redirect all non-www to www

      here is htaccess file

      Code:
      RewriteOptions inherit
      ### Begin - Inserted by Miva Merchant
      
      ##DirectoryIndex /merchant.mvc?Screen=SFNT
      
      RewriteEngine On
      
      RewriteRule ^admin.mvc? - [QSA,L]
      
      RewriteCond %{REQUEST_FILENAME} !-s
      RewriteRule ^product/([^/.]+).html$ /merchant.mvc?Screen=PROD&Product_code=$1 [QSA,L,NC]
      
      RewriteCond %{REQUEST_FILENAME} !-s
      RewriteRule ^category/([^/.]+).html$ /merchant.mvc?Screen=CTGY&Category_code=$1 [QSA,L,NC]
      
      RewriteCond %{REQUEST_FILENAME} !-s
      RewriteRule ^([^/]+)/([^/.]+).html$ /merchant.mvc?Screen=PROD&Category_code=$1&Product_code=$2 [QSA,L,NC]
      
      RewriteCond %{REQUEST_FILENAME} !-s
      RewriteRule ^([^/.]+).html$ /merchant.mvc?Screen=$1 [QSA,L,NC]
      
      ### End - Inserted by Miva Merchant
      
      RewriteCond %{HTTP_HOST} ^mysite.com\.com [NC]
      RewriteRule (.*) http://www.mysite.com/$1 [L,R=301]
      
      RewriteCond %{HTTP_HOST} ^mysite\.com$ [OR]
      RewriteCond %{HTTP_HOST} ^www\.mysite\.com$
      RewriteRule ^index\.aspx$ "http\:\/\/www\.mysite\.com\/SFNT\.html" [R=301,L]
      
      RewriteCond %{HTTP_HOST} ^mysite\.com$ [OR]
      RewriteCond %{HTTP_HOST} ^www\.mysite\.com$
      RewriteRule ^browse\/categories\.aspx$ "http\:\/\/www\.mysite\.com\/SFNT\.html" [R=301,L]
      
      RewriteCond %{HTTP_HOST} ^.*$
      RewriteRule ^browse\/categories\.aspx$ "http\:\/\/www\.mysite\.com\/SFNT\.html" [R=301,L]
      
      ## categories
      # Repeat the below as needed for other strings
      RewriteCond %{QUERY_STRING} cid=55&bid=2
      RewriteRule ^(.*)$ http://www.mysite.com/category/books.html? [R=301,L]
      RewriteCond %{QUERY_STRING} cid=55&bid=2
      RewriteRule ^(.*)$ http://www.mysite.com/category/dvds.html? [R=301,L]
      RewriteCond %{QUERY_STRING} cid=57&bid=2
      RewriteRule ^(.*)$ http://www.mysite.com/category/cds.html? [R=301,L]
      
      #products
      RewriteCond %{QUERY_STRING} cid=57&eid=904&bid=2&pgid=0&gid=-1&sid=-1&viewmode=Paged
      RewriteRule ^(.*)$ http://www.mysite.com/dvds/thor.html? [R=301,L]
      
      ## static pages
      Redirect 301 /contact.aspx http://www.mysite.com/ctus.html
      Redirect 301 /aboutus.aspx http://www.mysite.com/abus.html
      Redirect 301 /customerservice.aspx http://www.mysite.com/help.html
      Redirect 301 /privacy.aspx http://www.mysite.com/prpo.html
      Redirect 301 /faqs.aspx http://www.mysite.com/faqs.html
      Redirect 301 /sitemap.aspx http://www.mysite.com/smap.html
      Thanks again
      Keyboard Not Found..... Press F1 to continue

      Comment


        #4
        Re: redirect .aspx with query string to html page using htaccess

        The [L] means process no additional requests. Typically you want that, for performance reasons, and to avoid conflicts. It's odd that the non-www rewrite stopped working though; I'd have expected that if you came in on a non-www link, you'd first get the redirect to www and the url would otherwise stay the same, then the second redirection code would occur. I think I'd make the file look like this:

        Code:
        RewriteEngine On
        RewriteOptions inherit
        
        RewriteCond %{REQUEST_URI} ^/index.aspx$ [OR]
        RewriteCond %{REQUEST_URI} ^/browse/categories.aspx$
        RewriteRule .* http://www.mysite.com/SFNT.html [R=301,L]
        
        ## categories
        # Repeat the below as needed for other strings
        RewriteCond %{QUERY_STRING} cid=55&bid=2
        RewriteRule ^(.*)$ http://www.mysite.com/category/books.html? [R=301,L]
        RewriteCond %{QUERY_STRING} cid=55&bid=2
        RewriteRule ^(.*)$ http://www.mysite.com/category/dvds.html? [R=301,L]
        RewriteCond %{QUERY_STRING} cid=57&bid=2
        RewriteRule ^(.*)$ http://www.mysite.com/category/cds.html? [R=301,L]
        
        #products
        RewriteCond %{QUERY_STRING} cid=57&eid=904&bid=2&pgid=0&gid=-1&sid=-1&viewmode=Paged
        RewriteRule ^(.*)$ http://www.mysite.com/dvds/thor.html? [R=301,L]
        
        RewriteCond %{HTTP_HOST} !^www.mysite.com\.com [NC]
        RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
        
        ### Begin - Inserted by Miva Merchant
        
        ##DirectoryIndex /merchant.mvc?Screen=SFNT
        
        RewriteRule ^admin.mvc? - [QSA,L]
        
        RewriteCond %{REQUEST_FILENAME} !-s
        RewriteRule ^product/([^/.]+).html$ /merchant.mvc?Screen=PROD&Product_code=$1 [QSA,L,NC]
        
        RewriteCond %{REQUEST_FILENAME} !-s
        RewriteRule ^category/([^/.]+).html$ /merchant.mvc?Screen=CTGY&Category_code=$1 [QSA,L,NC]
        
        RewriteCond %{REQUEST_FILENAME} !-s
        RewriteRule ^([^/]+)/([^/.]+).html$ /merchant.mvc?Screen=PROD&Category_code=$1&Product_code=$2 [QSA,L,NC]
        
        RewriteCond %{REQUEST_FILENAME} !-s
        RewriteRule ^([^/.]+).html$ /merchant.mvc?Screen=$1 [QSA,L,NC]
        
        ### End - Inserted by Miva Merchant
        
        ## static pages
        Redirect 301 /contact.aspx http://www.mysite.com/ctus.html
        Redirect 301 /aboutus.aspx http://www.mysite.com/abus.html
        Redirect 301 /customerservice.aspx http://www.mysite.com/help.html
        Redirect 301 /privacy.aspx http://www.mysite.com/prpo.html
        Redirect 301 /faqs.aspx http://www.mysite.com/faqs.html
        Redirect 301 /sitemap.aspx http://www.mysite.com/smap.html
        David Hubbard
        CIO
        Miva
        [email protected]
        http://www.miva.com

        Comment


          #5
          Re: redirect .aspx with query string to html page using htaccess

          Thanks and appreciate it a lot, but When I used that code, the sites goes a redirect loop and displayed a message saying problem loading the site due to redirect loop.
          Keyboard Not Found..... Press F1 to continue

          Comment


            #6
            Re: redirect .aspx with query string to html page using htaccess

            Any chance something in the store or in the web server config is configured to serve the site without the www?
            David Hubbard
            CIO
            Miva
            [email protected]
            http://www.miva.com

            Comment


              #7
              Re: redirect .aspx with query string to html page using htaccess

              I am not sure, when I log into cpanel and under redirects I see these

              Code:
              RewriteCond %{HTTP_HOST} ^mysite\.com$ [OR]
              RewriteCond %{HTTP_HOST} ^www\.mysite\.com$
              RewriteRule ^index\.aspx$ "http\:\/\/www\.mysite\.com\/SFNT\.html" [R=301,L]
              
              RewriteCond %{HTTP_HOST} ^.*$
              RewriteRule ^browse\/categories\.aspx$ "http\:\/\/www\.mysite\.com\/SFNT\.html" [R=301,L]
              but nothing with non-www to www. We do have dev site as test.mysite.com

              Thanks
              Keyboard Not Found..... Press F1 to continue

              Comment


                #8
                Re: redirect .aspx with query string to html page using htaccess

                Might want to delete the RewriteOptions inherit line. There may be other rewrites coming from the server config that you don't need that are affecting the .htaccess file.
                David Hubbard
                CIO
                Miva
                [email protected]
                http://www.miva.com

                Comment


                  #9
                  Re: redirect .aspx with query string to html page using htaccess

                  Thanks for replying and that did the trick. Appreciate it.

                  We have changed our SSL from https://secure.mysite.com to https://www.mysite.com

                  How can I add a redirect in this case

                  Thanks once again
                  Keyboard Not Found..... Press F1 to continue

                  Comment


                    #10
                    Re: redirect .aspx with query string to html page using htaccess

                    There are two ways to do that:

                    1) If https://secure.mysite.com still exists as a separate site, throw a .htaccess in with:

                    RedirectPermanent https://www.mysite.com/

                    The browser will put the original request back on so everything should work like before.

                    2) If https://secure.mysite.com no longer exists, then it gets kind of ugly. Your existing rewrite should already be handling that:

                    RewriteCond %{HTTP_HOST} !^www.mysite.com\.com [NC]
                    RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]

                    Since that will force anything other than www.mysite.com to become www.mysite.com. The problem is that if the SSL certificate you have for www.mysite.com is strictly for just 'www.mysite.com' or both www.mysite.com and mysite.com, but NOT secure.mysite.com, then you're going to get an error regardless of what you do. SSL certificates have to negotiate before the request is sent to the server, so the client, thinking it wants to go to secure.mysite.com, is going to pop up a warning that the server does not have an SSL certificate for the domain secure.mysite.com and an error will result. The only way around that is to purchase an expensive wildcard SSL certificate for *.mysite.com which will work for everything, allow the redirect to occur and not produce an error. Those are generally in the $450+ range.

                    So, if this is the case, but you still have the old certificate, might be easier/cheaper to just add the old secure.mysite.com back in as a basic hosted domain just for the purposes of redirection using the old SSL.
                    David Hubbard
                    CIO
                    Miva
                    [email protected]
                    http://www.miva.com

                    Comment


                      #11
                      Re: redirect .aspx with query string to html page using htaccess

                      Thanks again,

                      The https://secure.mysite.com is will expire this month end as long with our old Windows Server.

                      so until then if I add

                      Redirect 301 https://secure.mysite.com https://www.mysite.com

                      to the new site htaccess file which is hosted on Linux server, will it do the trick

                      Thanks
                      Keyboard Not Found..... Press F1 to continue

                      Comment


                        #12
                        Re: redirect .aspx with query string to html page using htaccess

                        I'm not sure how to do the equivalent of that on Windows. You could have a site added to the linux server as secure.mysite.com, add the SSL from the windows server, add the redirect, and just get rid of it once the cert expires since the visitor is going to get an error either way once that happens.

                        You don't need to add anything to the .htaccess of the primary site because it already has a redirect for anything that doesn't match www.mysite.com
                        David Hubbard
                        CIO
                        Miva
                        [email protected]
                        http://www.miva.com

                        Comment


                          #13
                          Re: redirect .aspx with query string to html page using htaccess

                          Simply Brilliant, That actually gave me an idea.

                          I updated the A record for the secure.mysite.com on the windows server to point to the new site IP. As that certificate expires in 10 days or so, I think it should be fine

                          Thanks once again for helping me out:)
                          Keyboard Not Found..... Press F1 to continue

                          Comment

                          Working...
                          X