Radim
Radim

Reputation: 125

Handle http requests for any page by one php file and redirect from index.html

I have a page that works with one PHP script, I have a working redirect, but since the previous version always had an index.html file at the end, I would need to redirect queries to the parent folder:

example.com/index.html -> example.com/

example.com/folder/index.html -> example.com/folder/

My current listing looks like this:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^.*$ https://%1/$1 [R=301,L]
RewriteCond $1 !index\.php
RewriteRule ^(.*)$ index.php [L]

And you wanted to add this, but then there's a loop

RewriteRule ^index.html$ / [L,R=301]
RewriteRule ^([a-zA-z0-9]+).html$ /$1   [L,R=301]

Please advise how to edit it? Thank you

Upvotes: 2

Views: 203

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133600

Considering that users are hitting index.html url from your browser and you want to rewrite and redirect it in backend to index.html if this is the case try.

RewriteEngine ON
RewriteCond %{THE_REQUEST} /index\.html\s [NC]
RewriteRule ^(.*)/index\.html/?$ /$1 [R=301,L,NC]

OR in case your URLs are user friendly URLs and you want to redirect in backend to index.html file then please try following. Considering you don't have trailing slashes, working on solution when trailing slashes are there too.

RewriteEngine ON
RewriteCond %{THE_REQUEST} /index\.html\s [NC]
RewriteRule ^(.*)$ /$1/index.html [NE,NC,L]

Upvotes: 2

Related Questions