Search code examples
apache.htaccessmod-rewritejoomlaexpressionengine

Apache .hatccess rewrites not working for legacy URLS


I'm trying to rewrite some legacy Joomla URLs on a site that's now using ExpressionEngine as its CMS but they're not working.

The ExpressionEngine URL rewrites, i.e. removing index.php from the URL work fine though.

This is what I've got:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTPS} !=on

# This is the one that's not working
  RewriteRule /index.php?option=com_chronocontact&Itemid=54 /contact/  [R=301,L]

# Force www
  RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
  RewriteCond %{HTTP_HOST} (.+)$ [NC]
  RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
# Redirect index.php Requests
  RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
  RewriteCond %{THE_REQUEST} ^GET
  RewriteRule ^index\.php(.+) $1 [R=301,L]
# Standard ExpressionEngine Rewrite
  RewriteCond %{REQUEST_URI} ^/
  RewriteCond %{QUERY_STRING} ^(gclid=.*)
  RewriteRule ^(.+) /index.php?/ [L,PT]
  RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond  $1 !^(assets|css|images|tinymce|js|min|cms|themes|index\.php|admin\.php|favicon\.ico|index\.php|path\.php|php\.ini) [NC]
  RewriteRule ^(.+) /index.php?/ [L]

</IfModule>

Can anyone spot what I'm doing wrong?


Solution

  • The first thing is the stray RewriteCond %{HTTPS} !=on that you have at the top. It looks like it belongs to the rule under it, as in:

    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
    RewriteCond %{HTTP_HOST} (.+)$ [NC]
    RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
    

    As far as the rule that you have commented that doesn't work, the ? is a reserved character for regular expressions, and your pattern actually says that the second p in /index.php is "optional". Additionally, you can't match against the query string in a rewrite rule, you need to use a rewrite condition and match against the %{QUERY_STRING} variable:

    RewriteCond %{QUERY_STRING} ^option=com_chronocontact&Itemid=54$
    RewriteRule ^(index.php)?$ /contact/? [R=301,L]
    

    is probably more along the lines of what you're looking for.