Htaccess redirect

Hello all. Apologies if this is duplicated. I have a beginner .htaccess question.

I have set up a site that uses .htaccess to redirect all traffic to the version of the site and that works fine.

I also have some code in the htaccess file that removes the file type so becomes, which also works well.

However the URL will still work, and I would like to make sure that all users see the URL.

So my question is (it’s only a very small site not using a CMS and will stay that way) - is the best way to achieve this to add content like this to the htaccess file:

redirect /house.html

or is the problem that the code I have used to change the URLs to remove the filetype incorrect?

Probably worth mentioning that the issue is part my fault - google and others picked up the site before I updated the htaccess, so now the house.html links are out there and I would like to ensure they get updated to /house instead.

Any advice on the best practice way to do this is appreciated.

Hi Matt,

First, you’ve asked what the problem is with your .htaccess (mod_rewrite) code but you did not show us your code so it’s rather difficult to answer in the blind.

I’m very surprised that you can serve house (unless you’ve enabled Options +MultiViews - argh!) but the tutorial I’ve had available to SitePointers for nearly 10 years has the mod_rewrite code to do just that (without MultiViews). Just look for the Redirect to New Format in the drop down menu and you’ll be taken straight to it.

You might benefit from reading the mod_rewrite tutorial at as it contains explanations and sample code. It’s helped many SitePoint members over the years and should help you,

As for your search engines, using R=301 redirections will auickly resolve that problem.



Hi DK,
Thanks a lot for your speedy reply, I will go and read your tutorial now.

For info this is the code I have used (taken from tutorial elsewhere) and I have made no changes to MultiViews…

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.html [NC,L]

And you will see that works fine. Does that code look sensible to you?

So the best thing to resolve the issue is:

redirect /house.html [R=301] ?

Many thanks for your help,
Best wishes,

I also tried to redirect the page using:

redirect /house.html [R=301] but that gave a 500 error.

Part of this is down to me not 100% understanding how the removal of the .html part of the URL works, so any advice is appreciated and that you for bearing with me with these no doubt basic questions.

I have been reading the mentioned tutorial and will continue to do so, although I need some regex practice too I think!


Hi Matt,

Well, I would NEVER use that code (but for minor reasons). However, your original post stated that you wanted to redirect TO house in order to serve house.html. That is a circular redirection which must be handled very carefully (and the code posted only handled the second redirection).

Further, Apache’s Redirect code:

[quote=]Redirect Directive
Description:	Sends an external redirect asking the client to fetch a different URL
Syntax:	Redirect [status] URL-path URL
Context:	server config, virtual host, directory, .htaccess
Override:	FileInfo
Status:	Base
Module:	mod_alias[/quote]

So, when you get the syntax wrong, 500 errors is all that you will get from the server. In other words …

Redirect 301 /house.html /house

… would have been syntactically correct but it would have prevented house.html from ever being served.

As stated above …

[quote= titled Redirect TO New Format]# assumes usable link is index.php?id=alpha
# and alpha is the extensionless link
# Redirect to NEW format
RewriteCond %{IS_SUBREQ} false
RewriteCond %{QUERY_STRING} id=([a-zA-Z]+)
RewriteRule ^index\.php$ %1? [R=301,L]

# Redirect back to "usable link"
RewriteRule ^([a-zA-Z]+)$ index.php? id=$1 [L]

That code is well commented and explained in the text.

Modifying that for your generic (lowercase) filenames:

# Assumes "usable link" is something.html
# and something is the extensionless link
# Redirect to NEW format
RewriteCond %{IS_SUBREQ} false
RewriteRule ^([a-z]+\.html$ %1 [R=301,L]

# Redirect back to "usable link"
RewriteRule ^([a-z]+)$ $1.html [L]

This non-looping “loopy redirection” is stopped from looping by the RewriteCond’s check for “already redirected” and will handle any of your lowercase.html files. I would also recommend that your redirection back include a check that the file exists as an html file with its own RewriteCond statement: The status code in the first RewriteRule will cause that redirection to be shown (and picked-up by search engines to update their database) while the lack of a status code in the second will prevent the redirection from being shown.

# Redirect back to "usable link" RewriteCond $1\.html -f RewriteRule ^([a-z]+)$ $1.html [L]

That’s only a bit more advanced than the tutorial but, if you’ve gone through it, you would fully understand each and every directive used - only the {IS_SUBREQ} is specific to this example.

WARNING: I always advise members NOT to “play script kiddie” and use code which is not understood. If you have questions, PLEASE be sure to ask before using in a production environment (test to your heart’s content offline but using code you do not understand online is a dangerous … and stupid … thing to do!).



1 Like

Hi DK,
Thanks again for taking the time to provide such a detailed response. I will read the tutorial again, and then look at implementing what you have suggested.

Totally agree about your final point too, a case of more haste less speed for sure, broke my own rules there too.

Many thanks again,


No worries, it’s my philosophy to “share the knowledge” …

… and, yeah, I am often in a “the hurrier I go, the behinder I get” mode, too.



This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.