Solved

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

Posted on 2011-09-05
8
290 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
 

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
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.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

863 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now