Forum Moderators: phranque
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
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.
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
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
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...]