Index.php rewrite in .htaccess


I’ve run into a peculiar problem. Our IT department got an alert logic notice about our Omeka-S site because it’s accessible via IP (and bypasses cloudflare), so they asked us to have the IP redirect to the domain in .htaccess using this directive, which I’ve seen as the common method:

RewriteCond %{HTTP_HOST} ^xxx\.xx\.x\.xx$
RewriteRule ^(.*)$$1 [R=301,L]

HOWEVER, when I applied it seems to conflict the omeka’s own rewriterule to index.php, ie:

RewriteCond %{ENV:REDIRECT_OMEKA_REQ} !"1"
RewriteRule ^(.*)$ %{ENV:BASE}/index.php [E=OMEKA_REQ:"1",L]

So when I go to IP address for the site it takes me to However, that gives me a 404 ‘Page not found’ error. I also tested the index.php on the sandbox and get same error:

Curious why does it have that directive if index.php doesn’t work?

I was able to get the desired result by changing that particular rewrite and eliminating index.php ie:

RewriteRule ^(.*)$ %{ENV:BASE}/ [E=OMEKA_REQ:"1",L]

BUT, would doing that be problematic for the rest of the site? I didn’t see any problems, but wanted to double check on that.


Joseph Anderson

Are you doing this as your first rule? Using basically your same rule I have no issues with it, as long as it’s the first one. You shouldn’t have to modify the core Omeka S rewrites to do a redirect: the redirect happens in a separate request, so it shouldn’t be interacting with the other rules really. But your redirect rule should be the first thing, right under RewriteEngine On.

You could also try a slight variation:

RewriteCond %{HTTP_HOST} ^xxx\.xx\.x\.xx$
RewriteRule ^(.*)${REQUEST_URI} [R=301,L]

which is maybe a little more “foolproof”: it will just pass along whatever path was on the incoming request, so sites in subdirectories or other situations would work correctly without needing changes.

That did the trick! It’s funny, at some point during my troubleshooting, I wondered if the order of the rules mattered but then I completely forgot that I thought that :expressionless:

Thanks John