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. Technical SEO Issues
    4. URL rewriting from subcategory to category

    URL rewriting from subcategory to category

    Technical SEO Issues
    7 3 2.2k
    • 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.
    • jkundrotas
      jkundrotas last edited by

      Hello everybody!

      I have quite simple question about URL rewriting from subcategory to category, yet I can't find any solution to this problem (due to lack of my deeper apache programming knowledge).

      Here is my problem/question:

      we have two website url structures that causes dublicate problems:

      1. www.website.lt/language/category/

      2. www.website.lt/language/category/1/

      1 and 2 pages are absolutely same (both also returns 200 OK). What we need is 301 redirect from 2 to 1 without any other deeper categories redirects (like www.website.com/language/category/1/169/ redirecting to .../category/1/ or .../category/).

      Here goes .htaccess URL rewrite rules:

      RewriteRule ^([^/]{1,3})/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ /index.php?lang=$1&idr=$2&par1=$3&par2=$4&par3=$5&par4=$6&%{QUERY_STRING} [L]

      RewriteRule ^([^/]{1,3})/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ /index.php?lang=$1&idr=$2&par1=$3&par2=$4&par3=$5&%{QUERY_STRING} [L]

      RewriteRule ^([^/]{1,3})/([^/]+)/([^/]+)/([^/]+)/$ /index.php?lang=$1&idr=$2&par1=$3&par2=$4&%{QUERY_STRING} [L]

      RewriteRule ^([^/]{1,3})/([^/]+)/([^/]+)/$ /index.php?lang=$1&idr=$2&par1=$3&%{QUERY_STRING} [L]

      RewriteRule ^([^/]{1,3})/([^/]+)/$ /index.php?lang=$1&idr=$2&%{QUERY_STRING} [L]

      RewriteRule ^([^/]{1,3})/$ /index.php?lang=$1&%{QUERY_STRING} [L]

      There are other redirects that handles non-www to www and related issues:

      RedirectMatch 301 ^/lt/$ http://www.domain.lt/

      RewriteCond %{HTTP_HOST} ^domain.lt

      RewriteRule (.*) http://www.domain.lt/$1 [R=301,L]

      RewriteCond %{REQUEST_FILENAME} !-f

      RewriteCond %{REQUEST_URI} !(.)/$RewriteRule ^(.)$ http://www.domain.lt/$1/ [R=301,L]

      At this moment we cannot solve this problem with rel canonical (due to our CMS limits).

      Thanks for your help guys!

      If You need any other details on our coding, just let me know.

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

        in windows on a IIS server it would be

        <match url="^category/1$"><action type="Rewrite" url="category"></action></match>

        i think in a htaccess file it would be sothing like

        RewriteRule ="^/category/1$ http://www.domain.lt/category [R=301,L]

        1 Reply Last reply Reply Quote 1
        • ShaMenz
          ShaMenz last edited by

          Hi kundrotas, The problem you have is that the code used for .htaccess functions is not terribly "intelligent". It does not allow for the use of "IF" statements etc. This being the case, the better option is to move the action to within the actual code. This is the rule at question RewriteRule ^([^/]{1,3})/([^/]+)/([^/]+)/$ /index.php?lang=$1&idr=$2&par1=$3&%{QUERY_STRING} [L] The easiest thing to do is in index.php add a check for par1 // If par1 is 1 and everything else is blank send it to the root. if( $par1 == '1' && $par2 == '' && $par3 == '' && $par4 == '' && $par5 == '' && $par6 == '' ) { $location = "/$lang/$idr/"; header ('HTTP/1.1 301 Moved Permanently'); header ('Location: '.$location); } Hope that helps, Sha

          jkundrotas 1 Reply Last reply Reply Quote 1
          • ShaMenz
            ShaMenz last edited by

            Sorry that my answer appears to have lost all line breaks...seems to be some css issues at the moment.

            Hoping you can copy it out and separate the lines ....I will try to reformat it as soon as I can, but right now it just keeps loading funky.

            Sha

            1 Reply Last reply Reply Quote 1
            • jkundrotas
              jkundrotas last edited by

              Thanks Alan Mosley and Sha Menz! We will try to implement given advices tonight (at low traffic hours) and check back soon with results. Sha, thats ok with line breaks, we will figure it out. Thanks for your support once again!

              1 Reply Last reply Reply Quote 0
              • jkundrotas
                jkundrotas @ShaMenz last edited by

                Thank You very much! The code You provided worked very well and solved our problem.

                ShaMenz 1 Reply Last reply Reply Quote 0
                • ShaMenz
                  ShaMenz @jkundrotas last edited by

                  That's great! 🙂 Glad to help anytime. Sha

                  1 Reply Last reply Reply Quote 0
                  • 1 / 1
                  • First post
                    Last post
                  • High DA url rewrite to your url...would it increase the Ranking of a website?
                    kayintveen_MD
                    kayintveen_MD
                    0
                    6
                    116

                  • Change in url structure - added category page
                    evolvingSEO
                    evolvingSEO
                    0
                    4
                    818

                  • How to find original URLS after Hosting Company added canonical URLs, URL rewrites and duplicate content.
                    Nobody1560986989723
                    Nobody1560986989723
                    0
                    2
                    366

                  • Crawl reveals hundreds of urls with multiple urls in the url string
                    irvingw
                    irvingw
                    0
                    5
                    455

                  • Difference between URL Rewrites and 301 Redirects for Rankings
                    Gaveltek-173238
                    Gaveltek-173238
                    0
                    4
                    570

                  • Sharepoint SEO - How to lowercase urls with rewrite rules?
                    aschottmuller
                    aschottmuller
                    0
                    6
                    2.9k

                  • URL Rewrite
                    oznappies
                    oznappies
                    0
                    4
                    1.0k

                  • URL rewrite question
                    danielmckay7
                    danielmckay7
                    0
                    6
                    890

                  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