0

I am attempting to control redirections on my site using the Apache vhosts config.

I have two-letter regions setup, such as /fr, /de, /es, which I am currently ignoring as you will see from my vhosts file below. However I also have a 301 permanent redirect setup to redirect /cm (and /fr/cm, /es/cm etc) to another page - this is controlled in my back-end system.

The complication is when I add the line RewriteCond %{REQUEST_URI} !^/cm this is caught by the RewriteCond, however I then cannot access /fr, /de, /es etc at all as they seem to get stuck in a loop and go to: www.ayrshireminis.com/fr/fr/fr/fr/fr/fr/fr/fr and then it HTTP 500 Internal Server Errors.

How could I change the RewriteCond to allow for:

  • /pa (this will be redirected to /minis, but it is controlled in the database and needs to stay as a valid URL)
  • /fr (the region)
  • /fr/pa
  • /es/pa

etc.

    # ----------------------------------------------------------------------
    # REGION SPECIFIC REDIRECTIONS
    # Below will remove region specific part of URL and load content from main UK site
    # ----------------------------------------------------------------------
    RewriteCond             %{REQUEST_URI}                  ^/([a-z]{2})$                   [OR]
    RewriteCond             %{REQUEST_URI}                  ^/([a-z]{2}/)(.*)$
    RewriteCond             %{REQUEST_URI}                  !^/cm
    RewriteRule             ^([a-z]{2}/|[a-z]{2})(.*)$      /$2                                             [L,QSA]

    # For VirtualDocumentRoot development instance configurations
    RewriteRule ^([^.]+)$ /index.php?url=$1 [L,QSA]
crmpicco
  • 16,605
  • 26
  • 134
  • 210
  • What URL are you going to that is causing you to get redirected to `www.ayrshireminis.com/fr/fr/fr/fr/fr/fr/fr/fr`? – Jon Lin Sep 24 '12 at 16:09

1 Answers1

0

Think I see your trouble here. The formatting of your question seemed to obscure the [OR] at the end of your first RewriteCond line and the [L,QSA] on the RewriteRule itself.

Here is what I suggest you try instead:

RewriteCond %{REQUEST_URI} !^/cm
RewriteCond %{REQUEST_URI} ^/([a-z]{2})/?(.*)$
RewriteRule .*   $2 [L,QSA]

I prefer to use a "guardian" pattern to throw out anything that will be untouched from the start. There seems to be an error in the logic of the directives you have listed. The first two directives seem to say "If the REQUEST_URI starts with only two characters OR it starts with only two characters, has a "/" and then whatever till the end, then continue with the processing."

If the REQUEST_URI was "/fr," it would match the first RewriteCond of the directives you included in your question. But if it did match this, then what would it do? If you are looking to rewrite to the second parenthetical grouping, and if there is no second parenthetical grouping detected by the pattern, then how could any redirection be done if there is nothing to redirect to? The request would then get passed on to subsequent Rewrite rules further in your configuration.

I also think the RewriteRule may be bad. In your current directives you likely should have:

RewriteRule ^/([a-z]{2}/|[a-z]{2})(.*)$ /$2 [L,QSA]

You probably should add in a leading "/" character, otherwise the "$2" will likely match to the entire REQUEST_URI. The REQUEST_URI should always start with a "/".

You probably already have tried this, but would suggest you enable more verbose logging, tail the log file to see if the patterns match or not while you enter them into a browser and hit the site. I currently use this:

RewriteEngine On
RewriteLog /usr/local/apache2/logs/rewrite.log
RewriteLogLevel 3

I cannot explain why you are ending up in the redirection loop, since the [L] should prevent this. Only thing I can think of, is if the other aspects of your application, outside of these directives, are involved in redirection, they may somehow be provoking this behavior, and feeding requests back into Apache. The more verbose logging may help isolate that, coupled with your access_log.

terryjbates
  • 312
  • 1
  • 8