htaccess - redirecting if no match

Ok, currently in my .htaccess file I have:

RewriteEngine on
RewriteRule ^([0-9]+)$ index.php?p=$1 [L]

It's designed so that if something like this comes in:

it will take that number, assign it to $p, and then run it through index.php so I can process it.

That works great and I need to keep that process.

Now.... what I would like to do is have any url that does not match that format ( to be forwarded to another domain intact (maintaining its directory structure).

So that:  is redirected to

and  is redirected to

and so on... but only if it does not match the type format.

Is that possible?

Thanks, Chris
Who is Participating?
arober11Connect With a Mentor Commented:
To permit index.php to be accessed and to offer a very slightly more efficiently solution, using %{REQUEST_URI} to avoid the slightly less efficient regexp logic above, you could use:

RewriteEngine on
RewriteRule ^([0-9]+)$ index.php?p=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*{REQUEST_URI} [L,R=301]

Open in new window

Add a fall through rule below the existing one.
Redirect ^(.*)$$1
RewriteRule ^(.*)$$1 [r=301,L]
St_Aug_Beach_BumAuthor Commented:
Thank you both very much.

I tested both of these and this is the one that worked out.

Appreciate it very much!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.