Solved

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

Posted on 2011-09-05
8
293 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
[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
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with mod_substitute 18 77
Expanding/Collapsing Tree on SharePoint List 29 62
Horizontal Full Calendar using php 5 42
Displaying Text in Columns in HTML 3 32
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

734 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