[htaccess] How to unset/delete 301 redirects

In my .htaccess I set a (couple) of 301 redirects. They are picked up only to create a infinite loop.

For example I set the following code:

Redirect 301 / http://%{HTTP_HOST}/nl

Which keeps the browser redirecting to http://example.com/nl -> http://example.com/nl/nl -> http://example.com/nl/nl/nl -> http://example.com/nl/nl/nl/nl -> etc.

Another one which keeps prepending www. to the domain name (http://www.www.www.www.www.example.com).

And a few other I set up trying to fix the mess. Of course the first thing I did was to revert .htaccess to the original format. However all redirects are still in effect! (on multiple browser/machines/“privacy browser”/IPS).

For testing tried adding this:

Redirect 301 /test http://www.google.com

This sends the browser to google if you go to example.com/test.

But if I change it to

Redirect 301 /test http://www.drupal.org

or

Redirect 302 /test http://www.drupal.org

or

RewriteCond %{REQUEST_URI} !^/$ [NC]
  RewriteRule ^ http://www.example.com [L,R=301]

or
remove the code completely.

It still keeps going to google instead of drupal.org

How do I get rid of (or override) these faulty redirects? :frowning:

Caching - your browser will be being clver andd trying to re-use what it found “last time” to save a new request - thats why it’ll keep going back to google.

As for the actual problem - a redirect on the root will always be caught and an attempt at re-direction made, / is valid for /nl. Try a full re-write rule instead of just a simple redirect?

But the redirect exists on browsers/computers who never even been to the website. How can they have it in their cache?

I tried a few like:

RewriteCond %{REQUEST_URI} ^/nl$ [NC]
RewriteRule ^ http://%{HTTP_HOST}/en/ [L,R=301]

and

RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule ^ http://%{HTTP_HOST}/nl/ [L,R=301]

and

RewriteCond %{HTTP_HOST} ^www\\.www [NC]
RewriteRule ^ http://www.example.com/en/ [L,R=301]

And others, but it doesn’t seem to do anything… :frowning:

GLM,

.htaccess is not cached so every shift-RELOADed request will receive fresh processing. Okay, that may not be true if you’re still using IE but real browsers don’t abuse the cache the way IE is know to do.

IMHO, what you need to do is create a specification for whatever it is you’re trying to do then walk through your code one pass at a time (as Apache does) until there are no more redirections. If you do that, you’ll understand the www.www.www and other loopy problems and learn to avoid them. Frankly, that’s the main problem with (.*) - it loops because noobie users have no clue how to exit the loop it creates.

Have a read of the tutorial linked in my signature for more mod_rewrite information.

Regards,

DK

I still don’t understand how to fix the problem.

I have four 301’s that I want to remove/change. But even with the Code Generator from your signature, I can’t redirect/delete them. :frowning:

Checked link: http://www.lebloncouture.com
Type of redirect: 301 Moved Permanently
Redirected to: http://www.www.lebloncouture.com/nl
Desired redirect: http://www.www.lebloncouture.com/nl/

Checked link: http://www.lebloncouture.com/nl
Type of redirect: 301 Moved Permanently
Redirected to: http://www.www.lebloncouture.com/nl
Desired redirect: none


Checked link: http://www.lebloncouture.com/nl/
Type of redirect: 301 Moved Permanently
Redirected to: http://lebloncouture.com/nl/
Desired redirect: none

Checked link: http://lebloncouture.com/nl/
Type of redirect: 301 Moved Permanently
Redirected to: http://www.lebloncouture.com/nl/
Desired redirect: none

GLM,

Regards,

DK

I’m sorry, my post wasn’t very clear.

The problem with the last two 301’s is that http://www.lebloncouture.com/nl/ redirects to http://lebloncouture.com/nl/
and http://lebloncouture.com/nl/ in turn redirects to http://www.lebloncouture.com/nl/. There by creating a loop.

As for http://www.lebloncouture.com/nl now redirects to http://WWW.WWW.lebloncouture.com/nl
Where the double www. in front of the domain is unwanted (and also keeps looping to add another www. in font of the previous www.

You solutions:

RewriteRule ^nl$ nl/ [R=301,L]
RewriteRule ^$ nl/ [R=301,L]

Work very well. But only as long as I use Ctrl+F5 to load a page (or add the header “Cache-Control: no-cache” to every browser request). After that

I tried to set the following headers in .htaccess:


ExpiresActive On
ExpiresDefault A0 
Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0"
Header Set Cache-Control "max-age=0, no-store"
Header Set Cache-Control "no-store"
Header set Pragma "no-cache"

Everyone seperate, all at once, none at all and different combinations. But none of them seem to make a difference. Once I stop using Ctrl+F5 the old 301 are once again in effect. :frowning:

For removing the multiple www.'s I tried the following codes:

RewriteCond %{HTTP_HOST} ^www\\.www\\. [NC]
RewriteRule ^ http://www.lebloncouture.com%{REQUEST_URI} [L,R=301]

and

RewriteCond %{HTTP_HOST} !^www\\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

and

RewriteCond %{HTTP_HOST} ^www\\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]

With no effect…

As stated before, you would gain enough knowledge to conquer these minor problem is you had read the tutorial.

Nevertheless, all you need is:

RewriteEngine on

# force (single) www
RewriteCond %{HTTP_HOST} !^www\\.lebloncouture\\.com$
RewriteRule .? http://www.lebloncouture.com%{REQUEST_URI} [R=301,L]

# force nl subdirectory
RewriteRule !^nl/ nl/%{REQUEST_URI} [R=301,L]

Regards,

DK

Thanks again, but it’s still not working. :frowning:

I tried using your code by itself

RewriteEngine on

# force (single) www
RewriteCond %{HTTP_HOST} !^www\\.lebloncouture\\.com$
RewriteRule .? http://www.lebloncouture.com%{REQUEST_URI} [R=301,L]

# force nl subdirectory
RewriteRule !^nl/ nl/%{REQUEST_URI} [R=301,L]

And also tried putting it I the current .htaccess which now looks like this:

#
# Apache/PHP/Drupal settings:
#

# Protect files and directories from prying eyes.
<FilesMatch "\\.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\\.php)?|xtmpl)$|^(\\..*|Entries.*|Repository|Root|Tag|Template)$">
  Order allow,deny
</FilesMatch>

# Don't show directory listings for URLs which map to a directory.
Options -Indexes

# Follow symbolic links in this directory.
Options +FollowSymLinks

# Make Drupal handle any 404 errors.
ErrorDocument 404 /index.php

# Set the default handler.
DirectoryIndex index.php index.html index.htm

# Override PHP settings that cannot be changed at runtime. See
# sites/default/default.settings.php and drupal_initialize_variables() in
# includes/bootstrap.inc for settings that can be changed at runtime.

# PHP 5, Apache 1 and 2.
<IfModule mod_php5.c>
  php_flag magic_quotes_gpc                 off
  php_flag magic_quotes_sybase              off
  php_flag register_globals                 off
  php_flag session.auto_start               off
  php_value mbstring.http_input             pass
  php_value mbstring.http_output            pass
  php_flag mbstring.encoding_translation    off
</IfModule>
php_flag display_errors on
php_value memory_limit 128M
# Requires mod_expires to be enabled.
<IfModule mod_expires.c>
  # Enable expirations.
  ExpiresActive On

  # Cache all files for 2 weeks after access (A).
  ExpiresDefault A0
  # 1209600

  <FilesMatch \\.php$>
    # Do not allow PHP scripts to be cached unless they explicitly send cache
    # headers themselves. Otherwise all scripts would have to overwrite the
    # headers set by mod_expires if they want another caching behavior. This may
    # fail if an error occurs early in the bootstrap process, and it may cause
    # problems if a non-Drupal PHP file is installed in a subdirectory.
    ExpiresActive Off
  </FilesMatch>
</IfModule>

# Various rewrite rules.
<IfModule mod_rewrite.c>
  RewriteEngine on

# force (single) www
RewriteCond %{HTTP_HOST} !^www\\.lebloncouture\\.com$
RewriteRule .? http://www.lebloncouture.com%{REQUEST_URI} [R=301,L]

# force nl subdirectory
RewriteRule !^nl/ nl/%{REQUEST_URI} [R=301,L]

  # Block access to "hidden" directories whose names begin with a period. This
  # includes directories used by version control systems such as Subversion or
  # Git to store control files. Files whose names begin with a period, as well
  # as the control files used by CVS, are protected by the FilesMatch directive
  # above.
  #
  # NOTE: This only works when mod_rewrite is loaded. Without mod_rewrite, it is
  # not possible to block access to entire directories from .htaccess, because
  # <DirectoryMatch> is not allowed here.
  #
  # If you do not have mod_rewrite installed, you should remove these
  # directories from your webroot or otherwise protect them from being
  # downloaded.
  RewriteRule "(^|/)\\." - [F]

  # 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/...)
  # uncomment the following:
  # RewriteCond %{HTTP_HOST} !^www\\. [NC]
  # RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [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 the following:
  # RewriteCond %{HTTP_HOST} ^www\\.(.+)$ [NC]
  # RewriteRule ^ http://%1%{REQUEST_URI} [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 /

  # Pass all requests not referring directly to files in the filesystem to
  # index.php. Clean URLs are handled in drupal_environment_initialize().
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^ index.php [L]

  # Rules to correctly serve gzip compressed CSS and JS files.
  # Requires both mod_rewrite and mod_headers to be enabled.
  <IfModule mod_headers.c>
    # Serve gzip compressed CSS files if they exist and the client accepts gzip.
    RewriteCond %{HTTP:Accept-encoding} gzip
    RewriteCond %{REQUEST_FILENAME}\\.gz -s
    RewriteRule ^(.*)\\.css $1\\.css\\.gz [QSA]

    # Serve gzip compressed JS files if they exist and the client accepts gzip.
    RewriteCond %{HTTP:Accept-encoding} gzip
    RewriteCond %{REQUEST_FILENAME}\\.gz -s
    RewriteRule ^(.*)\\.js $1\\.js\\.gz [QSA]

    # Serve correct content types, and prevent mod_deflate double gzip.
    RewriteRule \\.css\\.gz$ - [T=text/css,E=no-gzip:1]
    RewriteRule \\.js\\.gz$ - [T=text/javascript,E=no-gzip:1]

    <FilesMatch "(\\.js\\.gz|\\.css\\.gz)$">
      # Serve correct encoding type.
      Header set Content-Encoding gzip
      # Force proxies to cache gzipped & non-gzipped css/js files separately.
      Header append Vary Accept-Encoding
    </FilesMatch>
  </IfModule>
</IfModule>

Both work great if I use Ctrl+F5, but if just browse normally (on this computer or one that hasn’t been to the site before) it will still show the old 301 redirects/loops…

Did a search for other .htaccess files. There are a few shattered in other folders. They all contain one of the following:

SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006
Deny from all
Options None
Options +FollowSymLinks
<Files *>
  deny from all
</Files>

Neither of which should have effect on the root, right? Not is it in php code, because if I remove that it will make no difference (except give 404’s when I use Ctrl+F5, naturally).

Btw I’m using the browsers Chrome, FireFox and sometimes IE to test this.

GLM,

Unfortunately, I see too many things in your code which I would consider wrong but I only have time to make a few general comments on those. First, however, I believe I added an / where {REQUEST_URI} could already have that to let me correct my code:

RewriteEngine on

# force (single) www
RewriteCond %{HTTP_HOST} !^www\\.lebloncouture\\.com$
RewriteRule .? http://www.lebloncouture.com%{REQUEST_URI} [R=301,L]

# force nl subdirectory
RewriteRule !^nl/ nl[B][COLOR="#FF0000"]/[/COLOR][/B]%{REQUEST_URI} [R=301,L]
  1. When you have an Order allow,deny (in either order), I believe you must follow that with at least one statement, i.e., Allow all or Deny all or {more specificity on what you’re allowing or denying}. I’m surprised that Apache is not throwing an error with what seems to be a syntactical error like that.

  2. You need to learn not to abuse .htaccess files because they must be read and parsed multiple times for every file request made (including the supporting files, e.g., gif, jpg, css, js, etc). If you need things like your PHP directives, add them to the httpd.conf (or have your host do that for you). The same goes for long lists of IPs to ban as well as redirections.

  3. NEVER include IfModule wrappers in .htaccess (after verifying that a module exists). Every time they’re shown, I reply with a standard rant:

[rant #4][indent]The definition of an idiot is someone who repeatedly does the same thing expecting a different result. Asking Apache to confirm the existence of ANY module with an <IfModule> … </IfModule> wrapper is the same thing in the webmaster world. DON’T BE AN IDIOT! If you don’t know whether a module is enabled, run the test ONCE then REMOVE the wrapper as it is EXTREMELY wasteful of Apache’s resources (and should NEVER be allowed on a shared server).[/indent][/rant 4]

That’s not meant as a personal attack but as a wake-up call for novice webmasters. Please learn from it.

  1. I’m not used to seeing quotes in mod_rewrite code (although they can be used in RewriteCond statements) so RewriteRule “(^|/)\.” - [F] throws a red flag in front of my face. It says: ‘if the {REQUEST_URI} start with nothing or / followed by any character, FAIL the request.’ Nice: That should prevent anything being served by Apache.

  2. RewriteBase was designed to UNDO a mod_alias redirection so mod_rewrite can work on it. Where’s the Redirect statement? WHY use RewriteBase /?

  3. Where’s the regex within RewriteRule ^ index.php [L]? The start anchor matches exactly NOTHING so this is a ridiculous statement.

The junk at the bottom (other .htaccess files seem patently absurd.

Okay, with apologies for unloading like that, I realize that you’re blindly using code you know nothing about. Obviously, this is not conducive to good (or even usable) websites but there’s too much going on for me to try to correct any of it. The last time I did, the OP decided that it was more productive to argue about the code and I will not get into that ridiculous situation again.

Try what’s at the top of this post and it should do what you originally wanted. As for the remainder, I’ll leave it as an exercise for you to deal with - or someone from staff may be able to help you with the code.

Regards,

DK

Thanks again. The problem has been resolved! \o/

Only I’m not sure how, since I didn’t change anything yet since my last post. Personally I think the webhost flushed their caching server, but they deny that was the problem.

As for the rest of your comments. Interesting stuff! A lot to think about.

For starters I removed the <if something> statements. That makes perfect sense even if you know nothing about apache. I slap myself in the face for not doing that sooner.

About #6. doesn’t it mean it matched everything (as apposed to nothing)?

Since the site is running on Shared hosting I can’t change the httpd.conf.

I’m hesitating to change the rest, mostly because it took me almost two weeks to get the site up and running again since the last time I made changes… (even when I reverted to old code) and all the code is Drupal standard. Which by no means makes it the best or most efficient code, but it could be relate/depended on in other parts of Drupal. But I’ll definitely see in to it and learn about it. :slight_smile:

GLM,

Great! The “normal” problem with caching is that IE will do that and refuse to update. Don’t ask me why, it’s M$.

The “interesting stuff” comes from my attempt to help ALL members learn the nuances of mod_rewrite. Therefore, many of my comments are pedantic in nature but, failing to observe the “correct” coding can often cause problems.

No slapping, if you please! You’re learning and that’s the important thing.

#6 (lack of regex except for the start anchor) does NOT match anything. If you want to

For starters I removed the <if something> statements. That makes perfect sense even if you know nothing about apache. I slap myself in the face for not doing that sooner.

About #6. doesn’t it mean it matched everything (as apposed to nothing)? If you want to capture EVERYTHING (or nothing), then you can use the :kaioken: EVERYTHING :kaioken: atom, the (.*) … or realize that it’s already available as {REQUEST_URI} so all you need is regex which will always match, i.e., .? (i.e., an optional character - without any start or end anchor).

Have another read of the tutorial article. It was built to prevent carpal tunnel syndrome from answering the same questions over and over and … Those who’ve spent some time learning from it have commented that it really helped them understand mod_rewrite.

Regards,

DK