By rosslaird on
In my pre-drupal site, most of the important pages had a php extension. Now, in drupal, those same pages are nodes with clean urls. For example:
old site: http://www.rosslaird.info/books.php
drupal site: http://www.rosslaird.info/books
Now, I want all requests for the old pages (the php pages) to be automatically redirected to their new nodes. I believe this must be done in the .htaccess file, with rewrite rules, but I'm not sure how to do it. Most the of the rewrite tutorials I've seen are either too complex for me or too simple. My needs seem to be right in the middle.
Any tips or suggestions would be appreciated.
Thanks.
Ross
Comments
Try this
Put this before all the other rewrite rules in the standard .htaccess file that came with Drupal.
RewriteRule http://www.rosslaird.info/books.php http://www.rosslaird.info/books [R=301,L]
EDIT:
I just read your complete request. I guess what you really want is this:
RewriteCond %{REQUEST_URI} !.*index.php
RewriteRule ^(.*)\.php$ $1 [R=301,L]
In case it doesnt work:
The first line checks if the request goes to anything ending with index.php. If this is not the case the second request should rewrite everything in a way that drops the .php. It may not be correct though. I tend to mess these things up, but you can fiddle around with this until it works and post the solution here.
Andre
-------------------------------------------------
http://www.opentravelinfo.com
http://www.aguntherphotography.com
Almost there
Hi Andre;
Thanks for the reply.
Your second solution *almost* works perfectly. For all php files in the root directory, it works. For all php files that are nested in directories below the root directory, it does not work. So, I suppose what I need is for the rewrite rule to truncate the path structure back to the root directory as required.
Hope that makes sense. I looked at the apache rewrite doc over at apache org, and I can see why the tutorial begins with this quote:
"Despite the tons of examples and docs, mod_rewrite is voodoo. Damned cool voodoo, but still voodoo.''
Ross
Question
I am not sure it makes sense.
do you want to reroute:
example.com/path1/blubb.php to example.com/path1/blubb or to example.com/blubb
If the first is the case, I would assume it works the way it is.
Where does this:
example.com/path1/blubb.php
send you with the rewrite rule above?
-------------------------------------------------
http://www.opentravelinfo.com
http://www.aguntherphotography.com
Using ErrorDocument
mod_rewrite can sometimes be slow, but most of the time is not noticable.
Another strategy would be to use the Apache's ErrorDocument directive. You can create an array (or go so far as to create a database table to perform lookups) to match requested URLs, and if they have moved, redirect them to the correct page.
__
Personal home page | Rhombic Networks: Drupal-friendly web hosting
What?
Drupal has a builtin path alias table.
I have migrated my whole photo website from a static page to drupal and have recreated every single path and file with this. What I couldn't recreate I wrote a few rewrites for.
This file does not exist:
http://www.aguntherphotography.com/galleries/south_america/peru.html
Its simply a path alias for some node.
What you are suggesting sounds like going rabbit hunting with a cruise missile strapped to your back. That is of course if your host lets you access the apache conf. files in the first place.
Andre
-------------------------------------------------
http://www.opentravelinfo.com
http://www.aguntherphotography.com
Example in PHP
The OP did not ask about URL aliasing, he specifically asked about HTTP status 301 permanent redirects. Doing these kinds of redirects are useful because they get the original page delisted from search engines, and the new one listed.
You'd need .htaccess or Apache support whether you used mod_rewrite or the ErrorDocument directive, and more on that point: the ErrorDocument is part of Apache core, and mod_rewrite is not, so it's also more likely to be available.
Code for this hypothetical script (I've not checked if it works):
New, relevant handbook article
Enable default 404 handling for some file types
__
Personal home page | Rhombic Networks: Drupal-friendly web hosting