Link to home
Start Free TrialLog in
Avatar of EmeraldIsleNC
EmeraldIsleNCFlag for United States of America

asked on

Can I use .htaccess to redirect based on file type?

I have recently redeveloped a website that was originally done as an html-based side, but is now WordPress-powered (so, all php pages).

The old site was about 63 pages, and although I've put a redirect into each page via it's Dreamweaver template as a meta tag, I'm not happy with the fact that someone would actually have to load the old page to be redirected to the new one.

As such, I'm thinking if I could perform the redirect via the .htaccess file, that would be ideal.

Is it possible to use the .htaccess to redirect based on page type? For instance, can I tell it to redirect any page requests for .html files to the root web address?

Code examples would be appreciated.

Thanks!

Sara
Avatar of klalakomacoi
klalakomacoi
Flag of Fiji image

Do you have mod_rewrite installed/turned on?
Avatar of EmeraldIsleNC

ASKER

I think so. How would I know for sure?

I mean, I already have a working .htaccess file in place for the WP permalinks, but I'm honestly kind of new to this whole .htaccess thing.
Avatar of degaray
degaray

just add one instance of the following line for each file. Backup your .htaccess prior to any modification.

Redirect /olddirectory/oldfile.htm /newdirectory/newphpfile.php
Actually, I have seen how to do that, I was wondering if there was a way to just redirect all .html files to the root web.

For instance, something like this:

redirect *.html http://www.domainname.com

Whereas all *.html files will automatically redirect to www.domainname.com. Does that make sense?

I just wanted to find out if this could be done before attempting to edit the file.
Try this:

Redirect (^.*).html http://www.domainname.com

If you want to do fancier things you should try to look for Regular Expresions (Regex)

You can google for a lot of tutorials.
Here you will find a Link for a cheat sheet: http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/
Here there is a regex tester: http://regexpal.com/
I tried that, changing the web address, of course, and it didn't do anything. I tried putting it at the end of my .htaccess file, and then I tried moving it to the beginning of the file.

Still had no effect.

Hmmm....?



Thanks for the links, btw.
Maybe this will help. Here is the original code for my .htaccess file:



# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Open in new window



So what should that file look like once I add the suggested statement?
ASKER CERTIFIED SOLUTION
Avatar of degaray
degaray

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