Link to home
Start Free TrialLog in
Avatar of Hube02
Hube02Flag for United States of America

asked on

rewrite to new URL if it exists?

Here's what I want to do, hopefully someone can tell me how to accomplish it.

Let's say that the requested url is http://somesite.com/page.html

with .htaccess see if the requested page does not exist.
if it does not exist then see if a php version of the same page does exist
then rewrite to the php page, if not then do nothiing

So if we have the following pages on the server:

page1.html
page2.php

a request for page1.html would not be rewritten
a request for page2.htm or page2.html would be rewritten as page2.php
a request for page3.htm would not be rewritten as the page does not exist.

Thanks for any help.

and if the
Avatar of jessc7
jessc7
Flag of United States of America image

Does example 5 (Redirecting to a 404 page if the directory and file do not exist) on the following page help?

http://www.sitepoint.com/article/apache-mod_rewrite-examples/2/

The exclamation point in the RewriteCond tells Apache to keep moving forward in the conditions if the file does not exist (!-f).
Are your pages specifically set up in the following format?

page1.html
page2.php

Or could it also be something like:

first.html
second.php

The pattern might make a difference on what your rewrite conditions would look like.
Actually, example 8 (Creating extensionless links) on the SitePoint article may have what you are looking for, but you would want to flip the php and html checks so that the conditions look for the HTML file first, and then fall over to the PHP file.
Avatar of Hube02

ASKER

What is happening is that we are converting the site from .html files to .php files. It is a huge site, more than 600 pages. We want to keep the .html extension in the address bar, thus the reason for using rewrite. However, the job is going to take some time to complete.

We will be renaming all the files from .html to .php, other than this the file names/folders will be identical.

So, I need to rewrite to the php file if the htm or html file does not exist and the php file does exist, but I do not want the rewrite to work if the php file does not exist.

I've looked at these examples, but they do not seem to be exactly what I'm looking for.

So you want the URL to always look like a .html is being server to the web browser, although it may be a .php file behind the scenes?

Are you renaming the .html files so they have exactly the same name as the old files, except they now have a .php extension?

If neither the .html file or .php file exist, are you ok with returning a 404 Not Found error?

What version of Apache are you using? Apache 1.x or 2.x?
Avatar of Hube02

ASKER

That's exactly what I'm looking for, I think.

for instance.

the browser will always show http://some.host/index.html

but the file that is actually used might be index.html or index.php, depending on which file actually exists on the server. Only one of the two should exist.
ASKER CERTIFIED SOLUTION
Avatar of jessc7
jessc7
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Hube02

ASKER

Perfect, I knew I needed to set some type of environment variable to get his to work, but I could not seem to do it. I tried back referencing a condition capture in the second condition, but that failed. Thank you for this.

Avatar of Hube02

ASKER

Perfect, Thanks again
Avatar of Hube02

ASKER

A slight modification, after doing some more testing it seems that the last 2 lines are not needed for my particular use.

RewriteRule ^(.*)\.html$ $1 [C,E=WasHTML:yes]
#rewrite to document.php if exists
RewriteCond %{REQUEST_FILENAME}.html !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]