Solved

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

Posted on 2011-09-05
8
289 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
Comment Utility
Do you have mod_rewrite installed/turned on?
0
 

Author Comment

by:EmeraldIsleNC
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 4

Expert Comment

by:degaray
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This video teaches users how to migrate an existing Wordpress website to a new domain.
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 …

771 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

10 Experts available now in Live!

Get 1:1 Help Now