Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

rewrite to new URL if it exists?

Posted on 2009-05-11
10
Medium Priority
?
428 Views
Last Modified: 2012-05-06
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
0
Comment
Question by:Hube02
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 16

Expert Comment

by:jessc7
ID: 24358578
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).
0
 
LVL 16

Expert Comment

by:jessc7
ID: 24358646
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.
0
 
LVL 16

Expert Comment

by:jessc7
ID: 24358722
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.
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 18

Author Comment

by:Hube02
ID: 24360113
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.

0
 
LVL 16

Expert Comment

by:jessc7
ID: 24360153
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?
0
 
LVL 18

Author Comment

by:Hube02
ID: 24360295
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.
0
 
LVL 16

Accepted Solution

by:
jessc7 earned 2000 total points
ID: 24360327
Hube02,

See if the follow rules work for you. I took the example at the following URL and modified it for your scenario (look for "Backward Compatibility for YYYY to XXXX migration"):

http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

I tested the rules on my server, and the following worked for me, with the URL path looking like an .html file:

http://<path>/test1.html
- test1.html (yes, served to browser)
- test1.php (no)

http://<path>/test2.html
- test2.php (yes)
- (no other files)

http://<path>/test3.html
- test3.html (yes)
- (no other files)

http://<path>/test4.html
- (no files, returns 404)
RewriteEngine On
RewriteBase /
 
#   parse out basename, but remember the fact
RewriteRule   ^(.*)\.html$              $1      [C,E=WasHTML:yes]
#   rewrite to document.phtml if exists
RewriteCond   %{REQUEST_FILENAME}.html !-f
RewriteCond   %{REQUEST_FILENAME}.php -f
RewriteRule   ^(.*)$ $1.php                   [S=1]
#   else reverse the previous basename cutout
RewriteCond   %{ENV:WasHTML}            ^yes$
RewriteRule   ^(.*)$ $1.html

Open in new window

0
 
LVL 18

Author Comment

by:Hube02
ID: 24363093
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.

0
 
LVL 18

Author Closing Comment

by:Hube02
ID: 31580309
Perfect, Thanks again
0
 
LVL 18

Author Comment

by:Hube02
ID: 24363149
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]
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question