The Moz Q&A Forum

    • Forum
    • Questions
    • My Q&A
    • Users
    • Ask the Community

    Welcome to the Q&A Forum

    Browse the forum for helpful insights and fresh discussions about all things SEO.

    1. SEO and Digital Marketing Q&A Forum
    2. Categories
    3. Link Building
    4. Urls rewriting "how to" with .htaccess

    Urls rewriting "how to" with .htaccess

    Link Building
    22 3 4.6k
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as question
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • baptisteplace
      baptisteplace @dephelis last edited by

      Can you be a bit more precise about the new url ? Does every old article with id has to 301 to a page with a unique name ?

      1 Reply Last reply Reply Quote 0
      • mozllo
        mozllo @dephelis last edited by

        Tks to you both  Baptiste placé Damiens Phillips and.

        What  do you mean when you say :

        "The redirect.php file will load the article (or category as I understood) and do a 301 to the new url."

        Is it en .htaccess file to create or a dedicated file.php , or both (redirect.php) ?

        Yes, i'll all have to transfer each old article  and i'll give them an unique urls per article..hope that reply your question !

        1 Reply Last reply Reply Quote 0
        • baptisteplace
          baptisteplace @dephelis last edited by

          To be clear about the different roles of the files in my solution, the .htaccess file will redirect every old url (whatever the id is) to a redirect script written in php.

          This script will get the old url Id, load the article (to get the article name) and then redirect 301 to the new url. Only in php can you access the database.

          Damien gave another solution, only based on htaccess. You have to write (or generate with code / software) 800 redirect directive for the htaccess file.

          1 Reply Last reply Reply Quote 2
          • dephelis
            dephelis @dephelis last edited by

            1. .htaccess won't exist on the windows platform unless you installed a rewrite mod on the windows server. If you did then the .htaccesswill be in the root folder of the website (usually) you should check the documentation of the rewrite mod to confirm that.

            2. If you have a windows PC then Xenu's Link Sleuth should be able to crawl the old site, you can then extract the information from the files that xenu can export.

            3/4. If every unique id needs to get mapped to a unique url then yes, 800 times it is. If you have multiple ids that go to the same page you could do:

            RewriteCond %{QUERY_STRING} ^id=113[3-8]$ [NC]

            RewriteRule ^newscontent.asp$ ^name-of-the-article$ [L,R=301]

            All ids from  1133 to 1138 will now redirect to the same page, you'll have to work out the regexs though.

            1 Reply Last reply Reply Quote 1
            • mozllo
              mozllo @dephelis last edited by

              Tks again, so (sorry to repeat)

              • your  solution : 1 .htaccess + redirect.php : located at the root of windows platform

              • Damien's : 1 .htaccess :located at the root of windows platform

              Is that correct ?

              1 Reply Last reply Reply Quote 0
              • dephelis
                dephelis @dephelis last edited by

                To be honest, this is the solution I'd go for.

                Mozollo, was your old site database driven?

                Are you using the old article titles as the new page names?

                If the answer is no to either of these, then the end result is you will have to manual map id to page name for each of the 800 pages you want to keep.

                1 Reply Last reply Reply Quote 0
                • mozllo
                  mozllo @dephelis last edited by

                  I'd say yes for the first one and for sure no for the second one...:)

                  1 Reply Last reply Reply Quote 0
                  • dephelis
                    dephelis @dephelis last edited by

                    OK in that case it simplifies things a bit.

                    In order to do any redirection from id=1136 to unique-article-name you will haveto create the mappings entirely manually.

                    The two solutions provided are:
                    Baptiste: On the new linux hosting set up an .htaccess file in the root of the site directory that redirects all id=xxxx requests to a redirect.php file on your server. The redirect.php file will need to interrogate a database with a table of the mappings and automatically redirect to the correct page via php scripting.

                    Mine: essentially the same as Baptiste's proposal, except that you don't interrogate the database, all the redirections are done using the htaccess file which contains all the mappings.

                    Either way you will need to manually create the mappings yourself, either in the database or in the htaccess file.

                    EDIT: Just had a thought, are the page titles of the articles the same between the new site and the old? If they are then you could crawl both sites with Xenu and then use vlookups in excel (or similar) to semi-automatically create your mapping of id = unique-article-name.

                    1 Reply Last reply Reply Quote 1
                    • baptisteplace
                      baptisteplace @dephelis last edited by

                      As you have only 800 urls, I agree with Damien, you should generate an associative array in pure php, associating every ID with the new url.

                      The redirect script will only test if the ID is an array key, if it is you 301 to the new url. Otherwise, display a 404 page.

                      1 Reply Last reply Reply Quote 1
                      • mozllo
                        mozllo @dephelis last edited by

                        Many tks for all these explanations..

                        So, in fact, lazily speaking, i would say that the .htaccess file solution give less work to do (no redirection script) and seems to be quite easy to make (excepting syntax inside .htaccess), so i 'll go for Damien's ..but i need credentials to install it.

                        Otherwise, if i don't, I'd go for Baptiste's...

                        Tks a lot...

                        1 Reply Last reply Reply Quote 0
                        • mozllo
                          mozllo @dephelis last edited by

                          Good idea..i'll to make it so , and use excel function.....tks

                          1 Reply Last reply Reply Quote 0
                          • mozllo
                            mozllo @dephelis last edited by

                            Hi, i was thinking of the whole picture of baptiste solution, you say :

                            "Baptiste: On the new linux hosting set up an .htaccess file in the root of the site directory that redirects all id=xxxx requests to a redirect.php file on your server. The redirect.php file will need to interrogate a database with a table of the mappings and automatically redirect to the correct page via php scripting."

                            it means that wiithout any credentials, any database access, if you have urls from the site you need to move to,  you can redirect any urls site to another one !?

                            Hum..i think i miss something ..

                            baptisteplace 1 Reply Last reply Reply Quote 0
                            • baptisteplace
                              baptisteplace @mozllo last edited by

                              You should get all the url of the old site with Xenu's Link Sleuth, then create a PHP array of oldUrl => newUrl and put it in your redirect script.

                              So you have in the htaccess :

                              RewriteCond %{REQUEST_URI}  ^/home/newscontent.asp
                              RewriteCond %{QUERY_STRING} id=([0-9]+)
                              RewriteRule ^(.*)$ redirect.php?id=%1 [L]

                              In the redirect.php file, you have :

                              $redirect = array("/home/newscontent.asp?id=1133" => "/name-of-the-article"); // 800 times (for all url)

                              if(isset($redirect[$_SERVER['REQUEST_URI']])) {
                                  header("Status: 301 Moved Permanently", false, 301);
                                  header("Location: http://www.mydomain.com/".$redirect[$_SERVER['REQUEST_URI']]);
                                  exit();
                              }

                              // Send a 404 if you don't have a redirect

                              1 Reply Last reply Reply Quote 0
                              • 1
                              • 2
                              • 1 / 2
                              • First post
                                Last post
                              • How do I set a link as a "do follow"?
                                Kuhliff
                                Kuhliff
                                0
                                4
                                45

                              • Links to "boring" pages
                                grobro
                                grobro
                                0
                                7
                                100

                              • "Bad" backlinks
                                hochzeitsfotograf
                                hochzeitsfotograf
                                0
                                6
                                1.2k

                              • Keyword + "intitle:resources"
                                BenFox
                                BenFox
                                0
                                2
                                481

                              • Bloggers "no_follow"
                                LushDuck
                                LushDuck
                                0
                                9
                                468

                              • How do you identify "Power Pages"
                                Nightwing
                                Nightwing
                                0
                                3
                                1.1k

                              • What are "naked backlinks"?
                                STPseo
                                STPseo
                                0
                                4
                                2.2k

                              • Posting "similar" blog posts to multiple blog sites seems "shady", does it work?
                                Timmmmy
                                Timmmmy
                                0
                                4
                                859

                              Get started with Moz Pro!

                              Unlock the power of advanced SEO tools and data-driven insights.

                              Start my free trial
                              Products
                              • Moz Pro
                              • Moz Local
                              • Moz API
                              • Moz Data
                              • STAT
                              • Product Updates
                              Moz Solutions
                              • SMB Solutions
                              • Agency Solutions
                              • Enterprise Solutions
                              • Digital Marketers
                              Free SEO Tools
                              • Domain Authority Checker
                              • Link Explorer
                              • Keyword Explorer
                              • Competitive Research
                              • Brand Authority Checker
                              • Local Citation Checker
                              • MozBar Extension
                              • MozCast
                              Resources
                              • Blog
                              • SEO Learning Center
                              • Help Hub
                              • Beginner's Guide to SEO
                              • How-to Guides
                              • Moz Academy
                              • API Docs
                              About Moz
                              • About
                              • Team
                              • Careers
                              • Contact
                              Why Moz
                              • Case Studies
                              • Testimonials
                              Get Involved
                              • Become an Affiliate
                              • MozCon
                              • Webinars
                              • Practical Marketer Series
                              • MozPod
                              Connect with us

                              Contact the Help team

                              Join our newsletter
                              Moz logo
                              © 2021 - 2026 SEOMoz, Inc., a Ziff Davis company. All rights reserved. Moz is a registered trademark of SEOMoz, Inc.
                              • Accessibility
                              • Terms of Use
                              • Privacy