0

I try to redirect the index.php plus system pages such as 404.php, 403.php etc. to their respective .html pages (index.html, 404.html, 403.html etc.) in the ROOT directory.

The problem is that the suggested solutions provided on Stackoverflow don’t seem to work in my case. Due to technical requirements, the PHP files have to stay in the root directory.

Rules in .htaccess

RewriteOptions inherit
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule . index.php [L]

</IfModule>
# -- concrete5 urls end --

<IfModule mod_headers.c>    
    Header set Access-Control-Allow-Origin *
</IfModule>
#Header set Access-Control-Allow-Origin *

1 Answer 1

1

Try this code at your main directory .htaccess file :

RewriteEngine On
RewriteRule ^(index|404|403)\.php$   /$1.html [L,R=302]

The above code will redirect mentioned pages only in root directory and if want to add sub directory to any one of them , just change it like this sub/index or sub/404 and so on.

I fit is ok , change 302 to 301 to get permanent redirection

Sign up to request clarification or add additional context in comments.

5 Comments

It partly worked. Everything redirects to index.html now. However if I access a folder in the php related sub-directory (root/folder) it also redirects back to index.html in root instead of the index.php in that sub-directory.
i am sure it is only for root but you may have other rules in sub directory, wait let me revise it
it should work in root only , let me know if you have any rule in .htaccess to subfolder or at that subfolder
In the subfolder there is no .htaccess file. I did edit my original post with the content of the existing .htaccess file. Something in there probably interferes with your solution.
i think you use aptche 2.4 so , RewriteOptions inherit will apply the root rules to sub directory before any rules , get it out and clear your browser cache and test it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.