We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14368
    • 56 Posts
    Hi all.

    I currently have friendly URLs and aliases turned on for my site which is working fine.
    However, I need to add some extra rewrite rules to provide redirects for the old version of the website, which has been replaced with the MODx version. I’m having a little bit of trouble getting these extra rules to work with the

    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]


    line which is the major one for Friendly URLs. If I add my extra rules after this, they don’t have any effect at all, presumably because the URL has already been internally rewritten to the index.php?q= format. If I place them before this line, my site loses its CSS and images for some reason.

    The extra lines I want to add go something like this:

    RewriteRule ^asd/([a-z]+).html$ /churches/asd/$1.html [R]
    RewriteRule ^anpd/([a-z]+).html$ /churches/anpd/$1.html [R]
    RewriteRule ^wad/([a-z]+).html$ /churches/wad/$1.html [R]


    Once they’ve been visibly rewritten in that format I am happy for MODx to internally rewrite them in the format it needs, but for some reason when this happens I lose images and CSS.

    Can anyone shed some light for me please?

    I’m using Apache and Revo version 2.0.7.

    Thanks
      • 13481
      • 97 Posts
      The reason why your rewrites don’t work after the FURLs one is because it has the "L" parameter which means that it will be the last match. Since it matches everything placing your rewrites below it won’t work.

      In order to figure out your css/image rewriting problem one would need to have your .htaccess file, knowledge of your file structure and an example of your html. I don’t think the general example you gave is enough information.

      James
        • 14368
        • 56 Posts
        Thanks for that James.

        The only other non-comment lines in my .htaccess file are
        RewriteEngine On
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d


        in other words just the standard Friendly URLs stuff so I figured I didn’t really need to include that.
        My site structure matches the RewriteRule format I posted, at least for that section of pages, e.g. /churches/asd/gawler.html.

        I’d prefer not to have to post the whole html source if I can avoid it, but I guess it’s probably the head section that’s most relevant, so here’s a snippet:

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
         
        <html xmlns="http://www.w3.org/1999/xhtml"> 
         
        <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
        <link rel="STYLESHEET" href="http://www.<domain name>.com/c/template.css" type="text/css"/> 
        <link rel="shortcut icon" href="favicon.ico"/> 
        <base href="http://www.<domain name>.com/" /> </head> 
         
        <body> 
        <img id="bannerBg" src="/i/banner-blank.png" width="1145" height="250" /> 
        


        is that enough? If not can you let me know what kinds of things you’re looking for please? Thanks again!
          • 14368
          • 56 Posts
          OK, after much frustration, learning about mod_rewrite, and experimentation, I think I’ve fixed it.

          I didn’t realise what the
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          rules were doing or how. Eventually figured out they mean ’don’t do any redirects for files/directories that really exist on the server’, i.e. my images and CSS files. Also, a big thing I didn’t get for ages was that you have to duplicate these conditions for each RewriteRule - so by adding another rule before the main index.php rule, I had inadvertently stopped the index.php rule from not redirecting my static files and that’s why my images and CSS weren’t showing - they were being redirected to index.php.

          My working .htaccess file now looks like this:
          RewriteEngine On
          RewriteBase /
          
          # Fix old church paths
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          
          RewriteRule ^(asd|anpd|wad)/([a-z]+).html$ churches/$1/$2.html [R=301,L]
          
          # Redirect friendly URLs to MODx internal format
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteCond %{REQUEST_URI} !^/index\.php$
          
          RewriteRule ^(.*)$ /index.php?q=$1 [L,QSA]