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. Intermediate & Advanced SEO
    4. Htaccess 301 regex question

    Htaccess 301 regex question

    Intermediate & Advanced SEO
    6 2 173
    • 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.
    • scanlin
      scanlin last edited by

      I need some help with a regex for htaccess. I want to 301 redirect this:

      http://olddomain.com/oldsubdir/fruit.aspx

      to this:

      https://www.newdomain.com/newsubdir/FRUIT

      changes:

      • different protocol (http -> https)
      • add 'www.'
      • different domain (olddomain and newdomain are constants)
      • different subdirectory (oldsubdir and newsubdir are constants)
      • 'fruit' is a variable (which will contain only letters [a-zA-Z])
      • is it possible to make 'fruit' UPPER case on the redirect (so 'fruit' -> 'FRUIT')
      • remove '.aspx'

      I think it's something like this (placed in the .htaccess file in the root directory of olddomain):

      RedirectMatch 301 /oldsubdir/(.*).aspx https://www.newdomain.com/newsubdir/$1

      Thanks.

      1 Reply Last reply Reply Quote 0
      • BlueprintMarketing
        BlueprintMarketing last edited by

        You would use

        <%@ Language="VBScript" %>
        <%
        ' ASP permanent URL redirect - generated by www.rapidtables.com
        Response.Status="301 Moved Permanently"
        Response.AddHeader "Location", "https://www.newdomain.com/newsubdir/FRUIT"
        Response.End
        %>

        try

        http://www.rapidtables.com/web/tools/redirect-generator.htm

        I hope this helps,

        Tom

        scanlin 1 Reply Last reply Reply Quote 0
        • scanlin
          scanlin @BlueprintMarketing last edited by

          Thanks, Tom. But 'fruit' was a placeholder for a variable page name that can contain letters or a '.' (sorry if that wasn't clear). for example, 'fruit' could be "abc.def.aspx" and I'd like it to become "ABC.DEF" (strip off the .aspx and uppercase it). Need a regex. The '.' within 'fruit' may or may not be present. But the page will always have the suffix '.aspx' that I want to strip off.

          I'd also like to do it with an .htaccess statement instead of VBScript. Running on unix.

          BlueprintMarketing 1 Reply Last reply Reply Quote 0
          • BlueprintMarketing
            BlueprintMarketing @scanlin last edited by

            I would use http://www.htaccessredirect.net/

            this can help to

            http://www.webconfs.com/how-to-redirect-a-webpage.php

            Let me know if you need anything

            scanlin 1 Reply Last reply Reply Quote 0
            • scanlin
              scanlin @BlueprintMarketing last edited by

              Hey Tom,

              I guess this was mostly a regex question. I think I figured it out:

              RewriteEngine On
              RewriteCond %{HTTP_HOST} ^olddomain.com$
              RewriteRule ^oldsubdir/([a-zA-Z.]+).aspx$ https://www.newdomain.com/newsubdir/$1 [R=301]

              Thanks for the tips.

              BlueprintMarketing 1 Reply Last reply Reply Quote 0
              • BlueprintMarketing
                BlueprintMarketing @scanlin last edited by

                Hi Mike,

                I am happy it worked out.

                you can use this to check as well

                http://seo-website-designer.com/Response-Redirect-Header-Checker

                All the best,

                Tom

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                • Flip-Flopping domains - 301 redirect question
                  OlegKorneitchouk
                  OlegKorneitchouk
                  0
                  6
                  272

                • Technical 301 question
                  seomozinator
                  seomozinator
                  0
                  10
                  128

                • Tricky 301 question
                  AlanMosley
                  AlanMosley
                  0
                  16
                  235

                • 301 or Canonical - Ecommerce Site Question
                  SylviaH
                  SylviaH
                  0
                  2
                  136

                • 301 Redirect htaccess
                  Christy-Correll
                  Christy-Correll
                  0
                  8
                  256

                • Canonical or 301 redirect, that is the question?
                  X-com
                  X-com
                  0
                  8
                  546

                • Questions about 301 Redirects
                  blacey
                  blacey
                  0
                  3
                  689

                • Question about 301 redirect for trailing / ?
                  YannickVeys
                  YannickVeys
                  0
                  4
                  524

                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