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?
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 theRewriteRule
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 singleRewriteRule
that follows. You've inserted theRewriteRule
between these directives, so you've effectively changed the logic. But these twoRewriteRule
directives could be easily combined and thatRewriteCond
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:
The pattern
^(forums/content/)?$
matches either/forums/content/
or simply/
, which is what the originalRewriteRule
was matching.I've removed the
RewriteCond
directive that checked againstHTTP_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:If you have existing
Options
directives then this can be combined, for example:Since
/forums
is a physical directory, if MultiViews is enabled then it can result in mod_negotiation making an internal request forcontent.php
(or even something else, likecontent.html
if it exists) before mod_rewrite is able to trigger the redirect.