1

I have inherited a site (a vbulletin forum) and migrated it from another hosting to mine, by copying everything with scp command. The root directory had the following .htaccess

RewriteOptions inherit

RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?$ "http\:\/\/example\.com\/forums\/content\/" [R=301,L]

I don't know how this was supposed to work, but it worked, regardless of the fact that the /forums/content/ folder did NOT exist. However, once moved to my hosting, it's stopped working, yielding a 404 error. Since the /forums/content.php file exists instead, I've edited the .htaccess like this:

RewriteOptions inherit

RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?$ "http\:\/\/example\.com\/forums\/content.php" [R=301,L]

Now it's working, with a glitch: my browser (and every other forum user browser) is caching the previous 301 redirect, so I can reach and use the forum only if I clear the browser cache (once), or if I enter the content.php URL by hand (every time).

I've tried the workaround to add a redirect from /forums/content/ to /forums/content.php, by adding a RewriteRule to .htaccess:

RewriteOptions inherit

RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/forums/content/$ "http\:\/\/example\.com\/forums\/content.php" [R=301,L]
RewriteRule ^/?$ "http\:\/\/example\.com\/forums\/content.php" [R=301,L]

However it seems that rule is ignored, because the browser still gets a 404 error on the /forums/content/ directory and it does not redirect to content.php. What am I doing wrong?

MrWhite
  • 12,647
  • 4
  • 29
  • 41
Lucio Crusca
  • 420
  • 3
  • 12
  • 33
  • 1
    There will be no leading `/` to match in the url when in a .htaccess file. Try `^forums/`... – meuh Jan 08 '17 at 19:32

1 Answers1

2

As meuh mentioned in comments, when mod_rewrite is used in per-directory .htaccess files, there is no leading slash prefix on the URL-path that is matched by the RewriteRule pattern. The directory-prefix (which always ends in a slash) is first removed.

However, your directives are also in the wrong order and could be tidied. The RewriteCond directive applies to the single RewriteRule that follows. You've inserted the RewriteRule between these directives, so you've effectively changed the logic. But these two RewriteRule directives could be easily combined and that RewriteCond directive looks superfluous.

Unless you specifically need the RewriteOptions directive, I would remove it. This might have been required on the previous host.

Try the following instead:

RewriteEngine on
RewriteRule ^(forums/content/)?$ http://example.com/forums/content.php [R=301,L]

The pattern ^(forums/content/)?$ matches either /forums/content/ or simply /, which is what the original RewriteRule was matching.

I've removed the RewriteCond directive that checked against HTTP_HOST. I assume it was supposed to have matched something (ie. not nothing). However, it simply matched anything which would always be true, which seems to be a bit pointless.

No need for all the backslash escaping in the RewriteRule substitution. This is an ordinary string, not a regex. (That sort of unnecessary escaping is typical of cPanel.)

UPDATE: You'll need to ensure that MultiViews is not enabled. This can be disabled with the Options directive near the top of your .htaccess file:

Options -MultiViews

If you have existing Options directives then this can be combined, for example:

Options -MultiViews +FollowSymLinks

Since /forums is a physical directory, if MultiViews is enabled then it can result in mod_negotiation making an internal request for content.php (or even something else, like content.html if it exists) before mod_rewrite is able to trigger the redirect.

MrWhite
  • 12,647
  • 4
  • 29
  • 41
  • A few days ago I accepted this answer, but I don't remember if I tried the solution before accepting: maybe I accepted it just because it seemed logic. However the solution is now in place, but I realize it is not working, because `/forums/content/` still yelds a 404. – Lucio Crusca Jan 18 '17 at 15:20
  • Presumably you are not being redirected to `/forums/content.php`? Do you have other directives in your `.htaccess` file? "I don't know how this was supposed to work" - from your original statement, if "/forums/content/" didn't exist as a physical directory then there must have been other directives in `.htaccess` that helped route the request (ie. a _front controller_)? – MrWhite Jan 18 '17 at 16:11
  • Maybe there were other directives outside of `.htaccess`. My current `.htaccess` contains only the two lines you suggested, and the hosting Apache instance is configured with a fairly plain VirtualHost with no redirects. BTW your solution DOES work when the browser opens `/`, but not when the browser opens `/forums/content/` – Lucio Crusca Jan 18 '17 at 20:42
  • It sounds like you might have MultiViews enabled. Try disabling it with `Options -MultiViews` near the top of your `.htaccess` file. (If you have an existing `Options` directive then you can combine it.) If MultiViews is enabled then it can result in mod_negotiation making an internal request for `content.php` (or something else) _before_ mod_rewrite is able to trigger the redirect. – MrWhite Jan 19 '17 at 01:37
  • 1
    Bingo! It works! Thanks a lot. If you edit your answer with the `Options -MultiViews` thing, I'll be glad to accept it again. – Lucio Crusca Jan 19 '17 at 12:18