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. How to 301 Redirect /page.php to /page, after a RewriteRule has already made /page.php accessible by /page (Getting errors)

    How to 301 Redirect /page.php to /page, after a RewriteRule has already made /page.php accessible by /page (Getting errors)

    Intermediate & Advanced SEO
    2 2 435
    • 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.
    • rcseo
      rcseo last edited by

      A site has its URLs with php extensions, like this: example.com/page.php

      I used the following rewrite to remove the extension so that the page can now be accessed from example.com/page

      RewriteCond %{REQUEST_FILENAME}.php -f
      RewriteRule ^(.*)$ $1.php [L]

      It works great. I can access it via the example.com/page URL.

      However, the problem is the page can still be accessed from example.com/page.php. Because I have external links going to the page, I want to 301 redirect example.com/page.php to example.com/page. I've tried this a couple of ways but I get redirect loops or 500 internal server errors.

      Is there a way to have both? Remove the extension and 301 the .php to no extension?

      By the way, if it matters, page.php is an actual file in the root directory (not created through another rewrite or URI routing).

      I'm hoping I can do this, and not just throw a example.com/page canonical tag on the page.

      Thanks!

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

        Hi,

        You might want to try this solution:

        Redirects domain.com/file.php to domain.com/file

        RewriteCond %{REQUEST_FILENAME} !-d                      # is not directory
        RewriteCond %{REQUEST_FILENAME}.php -f               # is an existing php file
        RewriteCond %{REQUEST_URI} ^(.+).php$                    # request URI ends with .php
        RewriteRule (.*).php$ /$1 [R=301,L]                                 # redirect from file.php to file

        Source: http://stackoverflow.com/questions/1992183/how-to-hide-the-html-extension-with-apache-mod-rewrite (2nd answer)

        Dirk

        1 Reply Last reply Reply Quote 1
        • 1 / 1
        • First post
          Last post
        • Would You Redirect a Page if the Parent Page was Redirected?
          Nozzle
          Nozzle
          0
          4
          92

        • 301 Redirect Only Home Page/Root Domain via Domain Registrar Only
          Roman-Delcarmen
          Roman-Delcarmen
          0
          4
          139

        • Soft 404 error for a big, longstanding 301-redirected page
          GlobeRunner
          GlobeRunner
          0
          2
          337

        • I've got duplicate pages. For example, blog/page/2 is the same as author/admin/page/2\. Is this something I should just ignore, or should I create the author/admin/page2 and then 301 redirect?
          BeanstalkIM
          BeanstalkIM
          0
          5
          414

        • 301 / 404 & Getting Rid of Keyword Pages
          DennisSeymour
          DennisSeymour
          0
          2
          92

        • What things should I consider if I am doing a 301 redirect on only 1 page/blog post?
          TheeDigital
          TheeDigital
          0
          3
          88

        • Urgent Site Migration Help: 301 redirect from legacy to new if legacy pages are NOT indexed but have links and domain/page authority of 50+?
          JDMcNamara
          JDMcNamara
          0
          7
          450

        • 301 - should I redirect entire domain or page for page?
          dunklea
          dunklea
          0
          4
          679

        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