Skip to content
    Moz logo Menu open Menu close
    • Products
      • Moz Pro
      • Moz Pro Home
      • Moz Local
      • Moz Local Home
      • STAT
      • Moz API
      • Moz API Home
      • Compare SEO Products
      • Moz Data
    • Free SEO Tools
      • Domain Analysis
      • Keyword Explorer
      • Link Explorer
      • Competitive Research
      • MozBar
      • More Free SEO Tools
    • Learn SEO
      • Beginner's Guide to SEO
      • SEO Learning Center
      • Moz Academy
      • SEO Q&A
      • Webinars, Whitepapers, & Guides
    • Blog
    • Why Moz
      • Agency Solutions
      • Enterprise Solutions
      • Small Business Solutions
      • Case Studies
      • The Moz Story
      • New Releases
    • Log in
    • Log out
    • Products
      • Moz Pro

        Your all-in-one suite of SEO essentials.

      • Moz Local

        Raise your local SEO visibility with complete local SEO management.

      • STAT

        SERP tracking and analytics for enterprise SEO experts.

      • Moz API

        Power your SEO with our index of over 44 trillion links.

      • Compare SEO Products

        See which Moz SEO solution best meets your business needs.

      • Moz Data

        Power your SEO strategy & AI models with custom data solutions.

      NEW Keyword Suggestions by Topic
      Moz Pro

      NEW Keyword Suggestions by Topic

      Learn more
    • Free SEO Tools
      • Domain Analysis

        Get top competitive SEO metrics like DA, top pages and more.

      • Keyword Explorer

        Find traffic-driving keywords with our 1.25 billion+ keyword index.

      • Link Explorer

        Explore over 40 trillion links for powerful backlink data.

      • Competitive Research

        Uncover valuable insights on your organic search competitors.

      • MozBar

        See top SEO metrics for free as you browse the web.

      • More Free SEO Tools

        Explore all the free SEO tools Moz has to offer.

      What is your Brand Authority?
      Moz

      What is your Brand Authority?

      Check yours now
    • Learn SEO
      • Beginner's Guide to SEO

        The #1 most popular introduction to SEO, trusted by millions.

      • SEO Learning Center

        Broaden your knowledge with SEO resources for all skill levels.

      • On-Demand Webinars

        Learn modern SEO best practices from industry experts.

      • How-To Guides

        Step-by-step guides to search success from the authority on SEO.

      • Moz Academy

        Upskill and get certified with on-demand courses & certifications.

      • SEO Q&A

        Insights & discussions from an SEO community of 500,000+.

      Unlock flexible pricing & new endpoints
      Moz API

      Unlock flexible pricing & new endpoints

      Find your plan
    • Blog
    • Why Moz
      • Small Business Solutions

        Uncover insights to make smarter marketing decisions in less time.

      • Agency Solutions

        Earn & keep valuable clients with unparalleled data & insights.

      • Enterprise Solutions

        Gain a competitive edge in the ever-changing world of search.

      • The Moz Story

        Moz was the first & remains the most trusted SEO company.

      • Case Studies

        Explore how Moz drives ROI with a proven track record of success.

      • New Releases

        Get the scoop on the latest and greatest from Moz.

      Surface actionable competitive intel
      New Feature

      Surface actionable competitive intel

      Learn More
    • Log in
      • Moz Pro
      • Moz Local
      • Moz Local Dashboard
      • Moz API
      • Moz API Dashboard
      • Moz Academy
    • Avatar
      • Moz Home
      • Notifications
      • Account & Billing
      • Manage Users
      • Community Profile
      • My Q&A
      • My Videos
      • Log Out

    The Moz Q&A Forum

    • Forum
    • Questions
    • Users
    • Ask the Community

    Welcome to the Q&A Forum

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

    1. Home
    2. SEO Tactics
    3. Link Building
    4. Urls rewriting "how to" with .htaccess

    Moz Q&A is closed.

    After more than 13 years, and tens of thousands of questions, Moz Q&A closed on 12th December 2024. Whilst we’re not completely removing the content - many posts will still be possible to view - we have locked both new posts and new replies. More details here.

    Urls rewriting "how to" with .htaccess

    Link Building
    3
    22
    8024
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as question
    Log in to reply
    This topic has been deleted. Only users with question management privileges can see it.
    • mozllo
      mozllo last edited by

      hi,

      Please i would need advices (links, tips, tool:generator ?) regarding url rewriting through .htaccess (newbee about it).

      It's a "refurbishing" website case  , the domain doesn't change. But the CMS does !

      I've got a list of urls (800) with which i don't want to loose rankings on :

      Here the type of old url syntax :

      http://www.mydomain.com/home/newscontent.asp?id=1133

      Here the new url type would be:

      http://www.mydomain.com/name-of-the-article
      or/and
      http://www.mydomain.com/category/Page-2

      Tks a lot...

      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
        • 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
          • 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

              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
              • 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
                • 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
                  • 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

                      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

                        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

                          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
                          • 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
                            • 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

                                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

                                  Hi,

                                  Tks to you both Damiens Phillips and Baptiste placé.

                                  But it seems to be a bit confusing for me for 2 reasons : language + technical knowledge !
                                  I confirm that i'll move from windows platform to linux one.

                                  So if i understand :

                                  1/ - htaccess is possible but where will it be located ? I assume at the root of the old platform (windows here..).

                                  2/ - I'll have to crawl each article in order to get each id (by the way, have you got any crawler tool to advise ?)

                                  3/ - For each of these urls i'll have to write such syntax :

                                  RewriteCond %{QUERY_STRING} ^id=1133$ [NC]
                                  RewriteRule ^newscontent.asp$ ^name-of-the-article$ [L,R=301]

                                  4/ ...800 times ? Or is there a way to tell on 1 line like :
                                  RewriteCond %{QUERY_STRING} ^id=1133$ + ^id=1134$ + ^id=1197$ ...... [NC]

                                  Tks a lot again

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

                                    I'll return the favour if it turns out he has moved from IIS 😉

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

                                      That's right but htaccess was asked. Thumbed up your answer so it goes first 😉

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

                                        But only if he's moved from Windows IIS hosting to Linux or Windows + PHP!

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

                                          True ! The good syntax is :

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

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

                                            He'll need to add [L,R=301] at the end instead of just [L]. IIRC default behaviour is a 302 redirect.

                                            You also can't reference a querystring in the RewriteRule, you have to use RewriteCond.

                                            baptisteplace dephelis mozllo 16 Replies Last reply Reply Quote 2
                                            • dephelis
                                              dephelis last edited by

                                              Hi,

                                              From the .asp in the sample URLs I'm guessing you're hosted on Windows, if that's the case you'll need to get a rewrite mod for IIS such as ISAPI Rewrite 3. We've been using it for about 5 years now and it performs well. Their site has documentation that shows how it works.

                                              You'll need to learn about regex expressions and a tool like Regex Buddy might be helpful.

                                              I'm not aware of an tools that can automate generation, and I think that in your case you're going to need to do some manual work to set it up.

                                              First you'll need a way of linking the old URLs to the new ones. Given the information you've provided, it's not clear how you'll be able to do this, so I'll make an assumption.

                                              Assuming that name-of-the-article is the same as the title of newscontent.asp?id=1133, you'll need to generate a list, in excel for example, that lists the old contentid and the title of that document. You can then use formulae/macros to generate the rewrite rules which you would enter in the .htaccess file.

                                              If you don't have a record of the id = title relationship in your old cms database (assumption!) then you might be able to do it by crawling the old site with a crawling program, exporting the data and then manipulating it. Otherwise you'll have to do it all by hand.

                                              Rewrite rules generally take the form:

                                              RewriteRule oldpageaddress newpageaddress [flags]

                                              You'll also need to use the RewriteCond in order to base the rule on the querystring.

                                              So for your example;

                                              RewriteCond %{QUERY_STRING} ^id=1133$ [NC]

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

                                              You'd then need to repeat those two statements for each page you want to redirect.

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

                                                Hi mozllo,

                                                You won't be able to create a .htaccess for such urls, because the original url only has the ID of the article and you want the name of the article in the new url. This requires database access to know the new url.

                                                I would suggest to put in your htaccess file :

                                                RewriteRule ^home/newscotnent.asp?id=([0-9]+) redirect.php?id=$1 [L]

                                                Edit : see good rule below

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

                                                dephelis 1 Reply Last reply Reply Quote 1
                                                • 1 / 1
                                                • First post
                                                  Last post

                                                Got a burning SEO question?

                                                Subscribe to Moz Pro to gain full access to Q&A, answer questions, and ask your own.


                                                Start my free trial


                                                Browse Questions

                                                Explore more categories

                                                • Moz Tools

                                                  Chat with the community about the Moz tools.

                                                • SEO Tactics

                                                  Discuss the SEO process with fellow marketers

                                                • Community

                                                  Discuss industry events, jobs, and news!

                                                • Digital Marketing

                                                  Chat about tactics outside of SEO

                                                • Research & Trends

                                                  Dive into research and trends in the search industry.

                                                • Support

                                                  Connect on product support and feature requests.

                                                • See all categories

                                                Related Questions

                                                • Kuhliff

                                                  How do I set a link as a "do follow"?

                                                  I have a page from a state agency linking to my website.  They have good PA and DA.  How do I tell google to "do follow"?  It is already showing up as linked to my site, I just want to make sure I'm getting the SEO juice! 🙂

                                                  Link Building | | Kuhliff
                                                  0
                                                • Sindre

                                                  My site is linked from over 900 "The Globe"-domains - disavow or not?

                                                  Hi! As some of you may be familiar with, there is a giant spam-network with the same design and same page title on all domains: "The Globe - The world's most visited web pages". For example: theglobe.at
                                                  theglobe.co.za
                                                  www.internet-search.us I've now discovered that over 900 of these spam-domains link to my site. My plan was of course to disavow them all, but Google is explicit on telling me to be careful. «Only disavow if you are certain that the links are damaging to your rankings». And to be honest, I'm not sure. Also: it seems drastic to disavow almost a thousand pages. I'm afraid to mess something up. What would you do?

                                                  Link Building | | Sindre
                                                  3
                                                • prozis

                                                  Empty href damages SEO? (href="#")

                                                  Hello, I'm analyzing a website with thousands of pages.
                                                  I realized that on many of them they have empty links such href="#". I wonder if that will cause any SEO damage, or if Google will just ignore it as there isn't any link? I was reading about it and people seems to not be sure, although they recommend on forums to user the CSS pointer clickable instead of empty link. Let me know your opinion on this please! Thank you in advance!

                                                  Link Building | | prozis
                                                  0
                                                • rflores

                                                  Does a text URL (without hyperlink) provide any SEO value?

                                                  Our clients use the link shortener of our service software to share their job offers. Sometime the job baords they post to do not allow hyperlink and thus post the url as text only. Do these url texts offer any benfits in terms of SEO?

                                                  Link Building | | rflores
                                                  0
                                                • sparrowdog

                                                  URL parameters affecting link juice

                                                  I have a couple of quirks in my online shop that I'm ironing out. One of them is adding some URL parameters to product links.ie: website.com/product.html?&cat=0&featured=Y If someone links to this URL, will I get the link juice as if it was website.com/product.html ? I have URL parameters in Webmaster Tools and robots.txt set up to ignore them so they're not in the Google index, but I have found a few websites that have linked to us using these longer URL's and I'm wondering whether to write to them and ask them if they mind changing them or not.

                                                  Link Building | | sparrowdog
                                                  0
                                                • OneMedical

                                                  A link with "return false"- OSE sees as a No Followed Link

                                                  Hello, I couldn't find a clear answer to the impact on SEO for a link written in this way: [" class="expert_info" onclick="window.open(this.href);return false;">](w</span>ww.yourwebsite.com<span style=) [Does the "return false" act as a "no follow"? I came across this in our link data in Open Site Explorer which lists these links all as "no follows." However, an engineer I spoke to said that it shouldn't impact search engine behavior. Any ideas? Thank you in advance! -Sarah K.](w</span>ww.yourwebsite.com<span style=)

                                                  Link Building | | OneMedical
                                                  0
                                                • tcolling

                                                  Does the ratio of external nofollow links to external "do follow" links matter in terms of SERPs ranking?

                                                  My site has an external link nofollow:dofollow ratio of approximately 1:1 That is, there are about as many nofollow external links as "do follow" external links. I have an impression that the ratio of no-follow to "do follow" links is a factor in the way that our website shows up in SERPs. I have the impression from reading a variety of sources, and from looking at Seomoz, that calculate "trust" factors as if they mattered (in SERPs), that seem to value a relatively low nofollow:dofollow ratio. Am I correct about that? Thanks,
                                                  Tim PS - I don't know whether or not this matters, but our website is at: www.trustworthycare.com - Tim

                                                  Link Building | | tcolling
                                                  0
                                                • lwilkins

                                                  Separate blog url helpful?

                                                  I am managing a website for a hotel brokerage firm. The main URL is www.hotelassetsgroup.com but we started a new blog with the URL www.hotelsforsaleblog.com.  We chose the blog URL because it has the words "hotels for sale." The main website has a tab at the top titled "Blog" which links to the blog URL. I am not sure if this is helping us or not because the main URL and blog URL are both competing for the name of the business "Hotel Assets Group." The blog has very little interlinking within the same URL because it mainly links to the main website. Should we combine the two URLs together and just have the blog URL be the same like- hotelassetsgroup.com/blog or something like that?

                                                  Link Building | | lwilkins
                                                  0

                                                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
                                                Free SEO Tools
                                                • Domain Authority Checker
                                                • Link Explorer
                                                • Keyword Explorer
                                                • Competitive Research
                                                • Brand Authority 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

                                                Access all your tools in one place. Whether you're tracking progress or analyzing data, everything you need is at your fingertips.

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

                                                Looks like your connection to Moz was lost, please wait while we try to reconnect.