Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

.htaccess RedirectMatch 301

6:06 pm on Jan 17, 2011 (gmt 0)

Full Member

5+ Year Member

joined:Aug 3, 2010
posts: 243
votes: 0


I've just switched one of my sites from .html to .php, so I can use require_once and other handy php features.

I tried using this code to redirect all .html to .php and ok it works but it breaks other sections of my site:

RedirectMatch 301 (.*)\.html$ [mysite.com$1.php<...]

The section is breaks is an ecommerce setup with RewriteBase > RewriteRules. These are in place for search engine friendly URLS.

RewriteRule ^(.*)-p-([0-9]+).html$ product_info.php?products_id=$2&%{QUERY_STRING}

So all the products in the store are becoming .php and as such do not work. I probably could change the .html$ to .php$ but I don't want to mess with that really.

Is there anyway to apply the RedirectMatch 301 to only a certain part of the site or tell it to ignore certain folders.

7:01 pm on Jan 17, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
votes: 0

Even though you now use PHP inside your files, you do not have to use ".php" in your URLs.

Keep both the same filenames and URLs as before, and set the server configuration to parse .html files for their PHP content.


Change the filenames to ".php" and use an internal rewrite such that when ".html" URLs are requested, the server looks for the correct .php file to serve the content.


Only as a last resort, change your URLs to use ".php" and set up a redirect so that when ".html" URLs are requested, they are redirected to the new URL.

One overall rule to apply is that if "any" of your rules use RewriteRule then use RewriteRule for "all" of the rules. That is, do not use Redirect or RedirectMatch at all. The reason is that you can't guarantee the order the rules will be parsed, as they are parsed "per module" and not in the actual order they appear in your configuration or .htaccess file.