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. 301 redirect rule

    301 redirect rule

    Intermediate & Advanced SEO
    8 3 343
    • 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.
    • iQandil
      iQandil last edited by

      Hi there,

      I have a website that has hundreds of links with a "question mark" at the end of URLs. For example:

      http://www.domain.com/directory/page.html?
      http://www.domain.com/directory/another-directory?
      http://www.domain.com/directory/yet-another-directory/?

      I'm want to place a wildcard redirect on the .htaccess file but don't know what exactly to add. Ideally I want the URLs above to be:

      http://www.domain.com/directory/page.html
      http://www.domain.com/directory/another-directory/
      http://www.domain.com/directory/yet-another-directory/

      Any help is most appreciated.

      Thanks
      Issa

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

        This should work..

        Test it out and see if we're on the right track.

        RewriteEngine on
        RedirectMatch 301 ^page.html?.$ http://www.domain.com/directory/page.html
        RedirectMatch 301 ^/another-directory?.
        $ http://www.domain.com/directory/another-directory/
        RedirectMatch 301 ^/yet-another-directory/?.*$ http://www.domain.com/directory/yet-another-directory/

        1 Reply Last reply Reply Quote 1
        • MichaelC-15022
          MichaelC-15022 last edited by

          Donford's answer looks right to me, but I'd test it on your particular server as well.

          My experience with .htaccess is that the various test tools don't always perform exactly the same as the server itself; also different webserver software (even different versions of Apache!) seem to have different results for the same patterns.

          So...it's not always even sufficient to test on your development server.

          iQandil 1 Reply Last reply Reply Quote 1
          • iQandil
            iQandil @MichaelC-15022 last edited by

            Not sure how the status of this question has changed to "Answered" but no thats not the right answer I'm afraid. What I was asking for is a wildcard redirect not redirect for the three examples i provided.

            As mentioned i have hundreds of pages that require this change.

            Issa

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

              the .*$ is the wildcard you can redirect on match keyword or directory.

              Here is a good site that helps with this, special note the "Wildcard Redirect" section.

              The question mark is usually an indication that the urls are dynamic if that is the case you may also want to review this helpful post on seOverflow.com about dynamic urls.

              MichaelC-15022 1 Reply Last reply Reply Quote 1
              • MichaelC-15022
                MichaelC-15022 @donford last edited by

                Donford's correct.  The "." matches any character; the "*" says 0 or more times; the $ means end of line.

                iQandil 1 Reply Last reply Reply Quote 0
                • iQandil
                  iQandil @MichaelC-15022 last edited by

                  Hi again,

                  Thank you for all this so far, however, it still doesn't help me very much. If i will need to follow the same system I will need to create a redirect line for each directory level and variation level I have on the website. Please not that in my initial question i said I have hundreds of cases of this issue.

                  I have discussed this issue with friends who came up with a solution that I thought i should share with you guys here. The redirect code should be:

                  RewriteCond %{THE_REQUEST} ^(GET|HEAD|POST)\ /[^\ ?]*?($|\ )

                  RewriteRule ^(.*)$ /$1? [L,R=301]

                  The first line ensures that the rule will only deal with question marks that appears at the end of the URL not the middle (as you kindly mentioned before). The second line then removes the question mark.

                  Plus, its just 2 lines, 1 rule which is a much shorter format

                  I hope this will be useful to someone else, it surely has saved me a lot of trouble, its now implemented and works perfectly.

                  Issa

                  MichaelC-15022 1 Reply Last reply Reply Quote 0
                  • MichaelC-15022
                    MichaelC-15022 @iQandil last edited by

                    Hmmmm...you shouldn't need the RewriteCond. The regex for the rule itself merely needs to end in ?$, indicating that the ? must be the last character on the line.

                    And your rule looks to me like it will APPEND a question mark, not remove it!

                    I haven't tested this, but this should work as a blanket rule for all files and folders:

                    RewriteRule ^(.*)?$ /$1 [L,R=301]

                    1 Reply Last reply Reply Quote 0
                    • 1 / 1
                    • First post
                      Last post
                    • 301 redirects
                      ThomasHarvey
                      ThomasHarvey
                      0
                      6
                      203

                    • 301 Redirects... Redirect all content at once or in increments?
                      Koki.Mourao
                      Koki.Mourao
                      0
                      6
                      175

                    • 301 Redirection
                      PremioOscar
                      PremioOscar
                      0
                      9
                      138

                    • 301 Redirects?
                      MickEdwards
                      MickEdwards
                      0
                      3
                      148

                    • 301 Redirect To Another 301 Redirect
                      cbielich
                      cbielich
                      0
                      5
                      247

                    • How to conduct catch 301 redirects & have the separate 301 redirects for the key pages
                      BenRWoodard
                      BenRWoodard
                      0
                      2
                      288

                    • After the 301 redirect
                      gazza777
                      gazza777
                      0
                      5
                      2.3k

                    • Multiple 301 redirects considered a redirection chain?
                      jmueller
                      jmueller
                      1
                      16
                      5.0k

                    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