0

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.

Urffly
  • 85
  • 1
  • 7

1 Answers1

1

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.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Now it throws a 500 error and in the log it says `unknown flag 'NE'`. May be some old/incompatible version of Apache? upd: docs say that Apache 2.2 should support 'NE' flag, now I am really confused. – Urffly Jul 07 '16 at 07:55
  • Sorry first line should have `NC` not `NE`, try updated rule – anubhava Jul 07 '16 at 08:01
  • well, it became even worse. Now `example.com` not redirects to `example.com/http` and `example.com/page.php` still throws a 404 error. – Urffly Jul 07 '16 at 08:07
  • I've done this and tested in a few browsers with cleared cache, and I've put your rule at the top of `.htaccess` and even try to delete everything in `.htaccess` but your rule. However it still does not work. May it be some glitch on server side? Should I contact my hoster or it's only trouble on my side? – Urffly Jul 07 '16 at 08:13
  • 2
    Why have you escaped the **$** @anubhava? – Amit Verma Jul 07 '16 at 08:15
  • 2
    Intended to escape DOT not `$` – anubhava Jul 07 '16 at 08:16
  • Finally it works, but also it looks like Apache redirects not only "user" request, but everything, so everything is broken. So it tries to grab CSSs not in `example.com/styles/style.css`, but in `example.com/http/styles/style.css`. I tried to solve the trouble by making redirects for `*.php` only, but failed. Any suggestions? – Urffly Jul 07 '16 at 08:43
  • Try my updated answer for matching `.php` request only. – anubhava Jul 07 '16 at 08:47
  • 1
    Thank you so much! I've made some corrections and it worked. I've changed `*.php` to `*.php|txt|xml` (for things like `sitemap`) and added `RewriteRule ^$ /http [L]`. – Urffly Jul 07 '16 at 08:56