Forum Moderators: phranque

Message Too Old, No Replies

Drupal Rewrite Conflict

Drupal Rewrite Conflict

         

asterix1

1:54 pm on Mar 31, 2006 (gmt 0)

10+ Year Member



Hello,

I am having trouble getting the following Rewrite Rules to work on my Drupal system (taken from this post - [forums.#*$!.com...]

## The basics to get mod_rewrite going ##
RewriteEngine on
RewriteBase /
## rewrite the index file in your root directory ##
## this should work, regardless if you have index.html, index.htm,
## or index.php
RewriteRule ^index\.(htm¦html¦php) [yourdomain.com...] [R=301,L]
## rewrite the index file in subdirectories
RewriteRule ^(.*)/index\.(htm¦html¦php) [yourdomain.com...] [R=301,L]

## match condition: skip any www requests
## or another way of saying this - match anything that doesn't start
## with www
RewriteCond %{HTTP_HOST}!^www\..*
## change any requests to www.yourdomain.com
## combined with the above RewriteCond means any
## non-www requests are redirected to www.yourdomain.com
## the %{REQUEST_URI} appends whatever url to www.yourdomain.com
RewriteRule ^.*$ [yourdomain.com%{REQUEST_URI}...] [R=permanent,L]

I think the reason it may not work is that Drupal has its own Rewrite rules (needed for SEF (clean) Url's):

# Rewrite current-style URLs of the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

The first two Rewrite rules I added work in that they redirect index.php to the root domain, however, somehow this also turns off the "clean URL's" (ie. they now show up in the form "/?q=node" instead of simply "/node".).

Finally, the last two lines, which serve to "send everything to www" cause an infinitely occurring loop - so that the URL shows domain/domain/domain. I have tried delving in to the RE's of Rewrite rules but am having a harder time than I anticipated. I'm guessing the Drupal RewriteRule is causing a conflict with the new RewriteRules?

Can anyone help?

Thanks for your time!
Alex

extras

3:18 pm on Mar 31, 2006 (gmt 0)

10+ Year Member



Putting this as the first rule will probabky stop the loop.

RewriteCond %{ENV:REDIRECT_STATUS} =200
RewriteRule ^ - [L]

If you are using php-cgi with Action directive, the code may need adjustment.

Another way is to use %{THE_REQUEST} to distinguish index.php from
the internal redirect and that of original reuquest.
But I think the ocde I showed is easier, in this case.

asterix1

3:41 pm on Mar 31, 2006 (gmt 0)

10+ Year Member



Awesome - that works for resolving index.php to the root, thx :)

What exactly does RewriteCond %{ENV:REDIRECT_STATUS} =200
RewriteRule ^ - [L] do? Is that an internal redirect?

I can't get the www. rewrite to work (domain.com -> www.domain.com), but I think that must be because I'm testing my site in a local environment (localhost) - and should work once online.

Cheers,
Alex

extras

3:49 pm on Mar 31, 2006 (gmt 0)

10+ Year Member



The drupla code makes internal redirect, and it creates REDIRECT_STATUS, REDIRECT_URL, and REDIRECT_QUERY_STRING.

So, you can use that for detecting if it's a rewritten URI or not,
similar to using THE_REQUEST.

www adding code shouldn't be affected with it.

asterix1

8:57 pm on Mar 31, 2006 (gmt 0)

10+ Year Member



Thanks for the quick reply!

I think I need to learn the syntax before I understand what's going on here. Now everything works, including www redirect - but it still won't work for subdirectories.

Also, I would like to redirect domain.com/node -> domain.com, but I can't seem to get this to work (probably very simple, but I'm still learning the syntax). I tried this but it didn't work:

RewriteRule ^/node [domain.com...] [R=301,L]

Any help is much appreciated!
Alex

extras

11:54 pm on Mar 31, 2006 (gmt 0)

10+ Year Member



This should do.
RewriteRule ^/*node [domain.com...] [R=301,L]

In the ,htaccess, dirctory part is removed before matching.
And for the doc root, it's /.

So. adding asteriks solves that issue and also covers the case
you get request for [ecample.com...]