Marcel Schmid Marcel Schmid - 1 year ago 101
Apache Configuration Question

redirect and change url from html to none html htaccess

I need to redirect domain.com/abcdef.html to domain.com/abcdef and the new url´s content should be domain.com/abcdef.html.

I realy struggle here. I can achieve to get the content from the html shown without having the html in the url, but then both urls are available.

When I try to achieve both I get an endlessloop and an ERR 500, but I have no clue how I can prevent this endlessloop.

so my try one - making both urls available with the same content looks like this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
RewriteRule ^([^\.]+)$ $1.html [NC,L]


The second rule is just the same withtout the first two lines:

RewriteRule ^(.*)\.html$ /$1 [L,R=301]
RewriteRule ^([^\.]+)$ $1.html [NC,L]


resulting into an endless loop. My research only brought me to solutions for either one or for a try with the index.php. But I search for a htaccess-only solution.

Is it possible?

EDIT: I understand why the endlessloop happens
I request blabla.html --> rule 1 makes it to blabla and requests again, rule 2 makes it to blabla.html requests again and so on. But how can I prevent this?

EDIT 2: So I tried as suggested by starkeen, and had this:

RewriteEngine On
RewriteCond %{ENV_REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteBase /
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
RewriteRule ^([^\.]+)$ $1.html [NC,L]


which still lead to the endlessloop. Using it with the RewriteCond still has both available (blabla.com/content.html and blabla.com/content)

Unfortunately it is not apache 2.4 so I couldnt try the END tag, but I try to reach my hoster and ask them if I they update it.

Or did I get you wrong and mix something up with the htaccess?

EDIT 3: So since my code should work, but doesnt, here the whole htaccess to make sure there isn´t anything else that blocks the **** from working.

AuthName "nottellingyou"
AuthType Basic
AuthUserFile /home/www/top/secret/.htpasswd
AuthGroupFile /dev/null
require valid-user

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{ENV_REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteBase /
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Answer Source

To prevent the loop error, put the following rule bellow RewriteEngine On line

RewriteCond %{ENV_REDIRECT_STATUS} 200
RewriteRule ^ - [L]

This terminates internal rewrite processing on 2nd iteration so /file.html will not get rewritten to the same path again.

On apach 2.4 you can use END flag in your RewriteRule to prevent the internal loop

RewriteRule ^([^.]+) $1.html [END]

To remove the .html extension, you can also use this script :

RewriteEngine on


#1)redirect /file.html to /file

RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NE,L,R]
 #2)check if /file exists as an .html ,if it exists ,rewrite /file to /file.html
RewriteCond %{REQUEST_FILENAME}.html -f

RewriteRule ^(.*?)/?$ /$1.html [L]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download