MODx htaccess problems and solutions


Yesterday I discovered that the flash banner wouldn’t load on www.asiandivingvacation.com, I suppose the crossdomain xml didn’t work like I expected it to.

Instead of messing around trying to make the crossdomain stuff work in Flash, I decided to just do a redirect like I have to do anyway and the problem would be solved. No one would be able to access the site through the www version anymore.

So I logged into cPanel and did the redirect, however when I loaded the page Apache complains that there is a never ending redirection loop in place and refuses to load it, great. I checked the redirection list in order to delete it and my new redirect is not there! So I opened a support ticket where I described the problem, this morning I got the answer from A2’s excellent support:

Hello,

It looks like there was an error in your .htaccess file, I have corrected the error and it now appears to be working as expected.

Thank you!
==============================================
Gerald Stuhrberg

And the page loads properly again. Gerald, I’m the one who should thank you for cleaning up my mess!

We still have to go to the bottom of this, here is the relevant lines in the .htaccess file that worked before I started redirecting in cPanel:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /asd
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA,NC]

Note the /asd part, originally I tried with just / which worked on localhost, but not A2.

Here is Gerald’s new version:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA,NC]
RewriteCond %{HTTP_HOST} ^www.asiandivingvacation.com$
RewriteRule ^/?(.*)$ "http\:\/\/asiandivingvacation\.com\/$1" [R=301,L]

Notice the two extra lines at the end combined with the original RewriteBase /. To be honest, Apache access files feels like a jungle, I can’t really explain 100% why this works so I’ll refrain. But at the moment it does with my MODx install, using pretty URLs and everything.

In cPanel’s addon domains there is also a redirect entry: http://asiandivingvacation.com/$1

Update: I’m using MODx for a project at work at the moment on a staging server that are riddled with symlinks. Commenting out Options +FollowSymlinks takes care of things though. It looks like this at the moment:

RewriteEngine On
RewriteBase /online/project_x
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA,NC]

In this case online is in the www folder.

I think I finally got a good explanation in the form of the .htaccess file that Drupal generates, short and concise:

...
  # If your site can be accessed both with and without the 'www.' prefix, you
  # can use one of the following settings to redirect users to your preferred
  # URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option:
  #
  # To redirect all users to access the site WITH the 'www.' prefix,
  # (http://example.com/... will be redirected to http://www.example.com/...)
  # adapt and uncomment the following:
  # RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
  # RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
  #
  # To redirect all users to access the site WITHOUT the 'www.' prefix,
  # (http://www.example.com/... will be redirected to http://example.com/...)
  # uncomment and adapt the following:
  # RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
  # RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

  # Modify the RewriteBase if you are using Drupal in a subdirectory or in a
  # VirtualDocumentRoot and the rewrite rules are not working properly.
  # For example if your site is at http://example.com/drupal uncomment and
  # modify the following line:
  # RewriteBase /drupal
  #
  # If your site is running in a VirtualDocumentRoot at http://example.com/,
  # uncomment the following line:
  # RewriteBase /
...

I just did this for a customer:

RewriteEngine on
RewriteBase /de

RewriteCond %{HTTP_HOST} ^www\.sea-bees\.de$ [NC]
RewriteRule ^(.*)$ http://www.sea-bees.com/de/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^sea-bees\.de$ [NC]
RewriteRule ^(.*)$ http://www.sea-bees.com/de/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^sea-bees\.com$ [NC]
RewriteRule ^(.*)$ http://www.sea-bees.com/de/$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /de/index.htm [L,R=301]

They’ve got the German version in a subfolder called /de which is accessible by www.sea-bees.com/de/, and www.sea-bees.de. We only want one version (.com) and therefore we redirect to it. Neither do we want direct access without the www.

The first cond/rule simply reroutes from .de to .com.

The second from .de without www to .com with www.

The third .com without www to .com with www.

And the final one which I now understand after consulting the mod_rewrite manual will reroute from just */de/ to */de/index.htm and only if we have just /de/. The way it does that is to check if REQUEST_FILENAME is a file or not, if it’s not (with the help of negation (!)) we do the rewrite.

That’s why for instance /de/welcome.htm wont be rerouted to /de/index.htm, because the -f test will return true for that one (welcome.htm is of course a valid file) and then the !-f test will return false.

Update: There was just a case of having to do a 301 redirect from one article at http://example.com/news/266 to http://example.com/news/nice-article where my SEO colleague finally made it work with the following rewrite rule:

RewriteRule ^(.*)news/shownews/266/$ http://example.com/news/shownews/1056/nice-article/ [L,R=301]

At first we had tried with a slash before news, like this: ^(.*)/news/shownews/266/$ but that didn’t work for some reason.


Related Posts

Tags: , , , ,