Loren McDonald
Loren McDonald

Reputation: 13

htaccess redirect specific files and folders to new a site AND anything else to a specific page on the new site

I can't find anyone asking or the answer to my specific scenario.

I am moving an old e-Commerce site with a ton of static product and info pages to a new e-Commerce software package installed in a new location (into a new folder in the root). http://domain.com into http://domain.com/newstore

I know I can redirect the whole site to the new location but that won't work since much of the new site runs dynamically from the eCommerce software and the product page addresses aren't even close to the old ones.

I have created individual rewrites using RedirectMatch for all the product pages I know need to be redirected so that is good. Now I need any pages I may have missed to redirect to the new store's home page but I obviously can't redirect what I don't know. So there's my issue, how do I now get any left over pages redirected to the home page without ruining the 200 some RedirectMatch rules I have in place?

Upvotes: 1

Views: 244

Answers (1)

Jon Lin
Jon Lin

Reputation: 143896

Note that RedirectMatch is mod_alias, not mod_rewrite. The two modules don't always play nicely with each other when directives from both get applied to the same URI. Without any idea how your site is setup, there's no way to answer your question, but you can try something like this using *mod_rewrite*:

RewriteEngine On
# Check that the request isn't for an existing resource
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Check that the request isn't for the new store
RewriteCond %{REQUEST_URI} !^/newstore
# redirect to the new store
RewriteRule ^ /newstore/ [L,R]

The reasoning here is that if a request isn't for an existing page, image, script, something that would possibly result in a 404, and if the request isn't for the dynamic site that is in the /newstore folder, then we redirect it to the /newstore/ folder.

But like I said, you've got 200 something RedirectMatch directives and mod_alias doesn't always play nice with mod_rewrite. You may need to translate them all into rewrite rules, but it's probably not necessary. Example:

RedirectMatch 301 ^/old-path/old-page.html$ /newstore/?page=123

would translate to:

RewriteRule ^/old-path/old-page.html$ /newstore/?page=123 [L,QSA,R=301]

All of these rewrite rules **must be before the rule above that redirects all 404's to /newstore*. Because it's a "catch-all" rule that is like a last resort, it needs to be after all of your regular redirects.

Upvotes: 1

Related Questions