Solved

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

Posted on 2011-09-05
8
291 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:EmeraldIsleNC
  • 4
  • 3
8 Comments
 
LVL 3

Expert Comment

by:klalakomacoi
ID: 36483739
Do you have mod_rewrite installed/turned on?
0
 

Author Comment

by:EmeraldIsleNC
ID: 36483758
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.
0
 
LVL 4

Expert Comment

by:degaray
ID: 36483824
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
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:EmeraldIsleNC
ID: 36483848
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.
0
 
LVL 4

Expert Comment

by:degaray
ID: 36484529
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/
0
 

Author Comment

by:EmeraldIsleNC
ID: 36484598
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.
0
 

Author Comment

by:EmeraldIsleNC
ID: 36484610
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?
0
 
LVL 4

Accepted Solution

by:
degaray earned 500 total points
ID: 36484976
Try to switch Redirect to RedirectMatch, like this:
# 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>

RedirectMatch /([^.]+).htm http://www.domain.com

# END WordPress

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Find out what you should include to make the best professional email signature for your organization.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

785 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