I am using D7.

I am attempting to create a redirect in my .htaccess file that will redirect the page if the path doesn't match a pattern.

# only if a particular domain
RewriteCond %{HTTP_HOST} ^mysite\.domain\.org$ [NC]
# only if URI contains "library"
RewriteCond %{REQUEST_URI} !^/Library$ [NC]
# rewrite the URL
RewriteRule ^(.*)$ http://otherdomain.org/$1 [R=301,L]

This works fine for most of the site (e.g. http://mysite.domain.org/blogs is redirected to http://otherdomain.org/blogs

However, http://mysite.domain.org/Library?id=1234 is being redirected but to http://otherdomain.org?id=1234 ("library" in URI is missing but this shouldn't redirect at all)

What am I missing? Am I running into Drupal URL rewriting rules?

  • Hello, and welcome to Drupal Answers. Did you write those rules before, or after the ones used by Drupal? – kiamlaluno Mar 18 '13 at 19:52
  • They come right before the index.php section – zkent Mar 18 '13 at 19:53
  • FYI. I couldn't get this to work but I am not sure if Drupal's own internal functions were interfering. We abandoned this for a different solution. – zkent Mar 20 '13 at 14:10

Try this:

# only if a particular domain
RewriteCond %{HTTP_HOST} ^mysite\.domain\.org$ [NC]
# rewrite the URL
RewriteRule ^((?!Library).*)$ http://otherdomain.org/$1 [R=301,L]

The (?!Library) should look ahead, and the match should fail if "Library" is present. Hope this works for you.

  • Getting a 500 error. RewriteRule: bad flag delimiters – zkent Mar 19 '13 at 1:39
  • Fixed. I had a space between the flags. – zkent Mar 19 '13 at 1:49
  • Its not failing with "library" in the URL. Its still rewriting the URL. – zkent Mar 19 '13 at 3:44
  • Library or library? It will be case sensitive. – Triskelion Mar 19 '13 at 4:03
  • 1
    oops. It's been too long since my Perl days. Try this instead: RewriteRule ^((?!Library).)*$ otherdomain.org/$1 [R=301,L]. – Triskelion Mar 19 '13 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.