apache.htaccesshttp-redirectpublic-html

Redirect everything to public_html


I can't believe I couldn't find the solution by myself, but here it is. I want Apache to redirect every request like example.com/page.php to example.com/http/page.php. I've found many options on the Internet and on StackOverflow, but none of them work. Now I got this:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com\$
RewriteRule (.*) http://example.com/http/$1 [R=301,L]
RewriteRule ^$ http [L]

And this redirects example.com to example.com/http but everything like example.com/some_page.php throws a 404 error. Other options either do not work at all or do the same thing. It's Apache/2.2.22 on CentOS, if this matters.


Solution

  • Try this rule:

    RewriteEngine on
    
    RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
    RewriteRule ^(?!http/).*\.php$ /http%{REQUEST_URI} [R=301,L,NE,NC]
    

    Make sure to clear your browser cache before testing this.